Reputation: 57271
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? E.g. 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
Upvotes: 1
Views: 974
Reputation: 6703
The value_counts()
function in pandas does this exactly.
Use that function on the column you want. i.e.
df['column_i_want'].value_counts()
Upvotes: 3