python - How to count occurrence of elements in a single column of Pandas Dataframe -
i have following df:
import pandas pd data = pd.dataframe({'name' : ['a', 'a', 'b', 'd']}) in [7]: data out[7]: name 0 1 2 b 3 d
what want summarize
a 2 b 1 d 1
how can it? tried not good:
g = data.groupby('name') g.count()
hope helps use value_counts:
data = pd.dataframe({'name' : ['a', 'a', 'b', 'd']}) in [14]: data['name'].value_counts() out[14]: 2 b 1 d 1 dtype: int64
or have tried using groupby:
g=data.groupby('name')['name'] g.count() out[25]: name 2 b 1 d 1 name: name, dtype: int64
Comments
Post a Comment