2

I have pandas DataFrame with many rows and columns, only three rows shown here:

date    place    number
2010    LON      10
2010    BER      20
2010    LON      5
2011    LON      10
2011    BER      15
2011    BER      10

I want to make a new dataframe which aggregates by year and sums up the number according to place:

date   place    number
2010   LON      15
2010   BER      20
2011   LON      10
2011   BER      25

I have tried many combinations of groupby with sum, inplace etc. But I cannot achieve this, any suggestions?

Ajean
  • 5,528
  • 14
  • 46
  • 69
user2052
  • 29
  • 1
  • 3

1 Answers1

4

You can try:

df.groupby(['date','place']).agg(sum)

Or If you want to get exactely the data frame as in your example:

df.groupby(['date','place']).agg(sum).reset_index()
milos.ai
  • 3,882
  • 7
  • 31
  • 33