-1

I have this dataframe:

    code
0   0000
1   0000  
2   0123  
3   0123
4   4567

I want to groupby code and count how many of each code there is in the dataframe, so it get to look like this:

   code  count     
0  0000      2 
1  0123      2
2  4567      1

I was using: group=df.groupby('code').agg('count') However I wasn't getting it right.

Can someone help?

aabujamra
  • 4,494
  • 13
  • 51
  • 101

1 Answers1

0

Had a similar problem and I'm forming @user3483203's comments into an answer so the question can be marked as answered.

Using:

df.groupby('code').size()

# OR

df.code.value_counts()

This groups all rows with matching codes and outputs the total for each code.

tda
  • 2,045
  • 1
  • 17
  • 42