我想计算每列中唯一值的数量,并仅选择那些唯一值少于 32 个的列。
我尝试使用
df.filter(nunique<32)
和
df[[ c for df.columns in df if c in c.nunique<32]]
Run Code Online (Sandbox Code Playgroud)
但是因为 nunique 是一种方法而不是函数,所以它们不起作用。认为 len(set() 会起作用并尝试过
df.apply(lambda x : len(set(x))
Run Code Online (Sandbox Code Playgroud)
但效果不佳。请问有什么想法吗?提前致谢!