0

I would like to add a count of the grouped rows with my groupby. Example:

name, amount
test, 1
test2, 5
test, 3
test3, 8
test2, 4
test, 1

Should give:

name, amount, count
test, 5, 3
test2, 9, 2
test3, 8, 1

My current groupby statements:

df = (df.groupby('name')['amount'].sum().reset_index().sort_values(by='name', ascending=True))
...
df = df.groupby(pd.cut(df['name'], 
           bins=[0,1,2], 
           labels=['A', 'B']))['amount'].sum().reset_index()
Scripter
  • 558
  • 2
  • 8
  • 20

0 Answers0