每个列数据框中的唯一身份用户数量

时间:2016-06-02 16:10:14

标签: python pandas dataframe unique series

是否有相当于nunique() in Series的数据帧,而不必遍历列?基本上确定每个数据帧列中唯一的数量,例如

  >>> df
   a  b
0  x  x
1  x  y
2  x  z
3  x  4

会给:

array([1, 4])

1 个答案:

答案 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]
相关问题