是否有相当于nunique() in Series的数据帧,而不必遍历列?基本上确定每个数据帧列中唯一的数量,例如
>>> df
a b
0 x x
1 x y
2 x z
3 x 4
会给:
array([1, 4])
答案 0 :(得分:2)
IIUC你可以使用apply
:
print (df.apply(lambda x: x.nunique()))
a 1
b 4
dtype: int64
print (df.apply(pd.Series.nunique))
a 1
b 4
dtype: int64
print (df.apply(lambda x: len(x.unique())))
a 1
b 4
dtype: int64
print (df.apply(lambda x: x.nunique()).values)
[1 4]