>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(index=range(10),data=np.random.randint(0,3,10),columns=['y'])
Gives,
>>> df.y
0 2
1 0
2 2
3 1
4 0
5 0
6 0
7 1
8 2
9 1
Name: y, dtype: int32
>>> df.groupby('y').count()
Empty DataFrame
Columns: []
Index: [0, 1, 2]
But I'm trying to get the count of every item in 'y'. Something like,
>>> from collections import Counter
>>> Counter(df.y)
Counter({0: 4, 1: 3, 2: 3})
Am I doing something wrong here?
Thanks!
Comment From: jreback
The index is 'eaten' by the groupby so you need to pick a column to count.
In [6]: df.groupby('y')['y'].count()
Out[6]:
y
0 1
1 5
2 4
Name: y, dtype: int64
you can also do this, treat as a series and groupby itself
In [11]: df['y'].groupby(df['y']).count()
Out[11]:
y
1 4
2 6
Name: y, dtype: int64
Comment From: jorisvandenbossche
Or even better, you can just use value_counts()
:
In [22]: df['y'].value_counts(sort=False)
Out[22]:
0 3
1 5
2 2
Name: y, dtype: int64