Shu Pan
Shu Pan

Reputation: 305

count entries from each group using pandas.dataframe.groupby.count

Note: I apologize for missing the quote which was an unintentional typo. It is corrected below. Thanks Beny for pointing out the typo.

I was trying to count the number of entries in each of group after applying groupby to a column, but got something unexplainable. Please help. Below is my code

data=[366, 366, 366, 366, 1, 2, 3]
df=pd.DataFrame(data, columns=['id'])
df.groupby('id').count()

and i got

id
1
2
3
366

I also found that ONLY if i do something like

df.groupby('id')['id'].count()

I got

id      count
1        1 
2        1
3        1 
366      4

Looks like because groupby was applied to 'id', therefore the agg functions could not be applied unless explicitly indicated.

Any explanation on this ? Thanks.

Upvotes: 0

Views: 1339

Answers (1)

BENY
BENY

Reputation: 323226

You should do double quote

df.groupby('id').size()
Out[106]: 
id
1      1
2      1
3      1
366    4
dtype: int64

When pass id , it is id of object not the id in your dataframe

More like

df.id.value_counts()
Out[107]: 
366    4
3      1
2      1
1      1
Name: id, dtype: int64

Upvotes: 1

Related Questions