【问题标题】:Pandas number of uniques in each column dataframe [duplicate]每列数据框中的 Pandas 唯一数[重复]
【发布时间】:2016-06-02 16:10:14
【问题描述】:

对于无需遍历列的数据帧,是否有 nunique() in Series 的等价物?基本上确定每个数据框列中唯一的数量,例如

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

会给:

array([1, 4])

【问题讨论】:

  • 您是否通过说“无需遍历列”来排除df.apply

标签: python pandas dataframe unique series


【解决方案1】:

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]

【讨论】:

  • 您可以使用count_values。例如df.column.count_values()
猜你喜欢
  • 2018-07-26
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多