【问题标题】:Fastest way to count number of occurrences in Pandas计算 Pandas 出现次数的最快方法
【发布时间】:2014-06-12 15:17:49
【问题描述】:

计算 Pandas 系列中元素出现次数的最快方法是什么?

我目前最快的解决方案是.groupby(columnname).size()。 Pandas 中有什么更快的东西吗?例如。我想要以下内容:

In [42]: df = DataFrame(['a', 'b', 'a'])

In [43]: df.groupby(0).size()
Out[43]: 
0
a    2
b    1
dtype: int64

【问题讨论】:

标签: python pandas


【解决方案1】:

pandas 中的 value_counts() 函数正是这样做的。

在您想要的列上使用该功能。 即

df['column_i_want'].value_counts()

【讨论】:

    猜你喜欢
    • 2020-02-22
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 2019-05-11
    • 2014-06-19
    相关资源
    最近更新 更多