简体   繁体   中英

Fastest way to count number of occurrences in Pandas

What is the fastest way to compute the number of occurrences of elements within a Pandas series?

My current fastest solution involves .groupby(columnname).size() . Is there anything faster within Pandas? Eg I want something like the following:

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

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

The value_counts() function in pandas does this exactly.

Use that function on the column you want. ie

df['column_i_want'].value_counts()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM