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?

1
  • Can you post what have you tried so far? Commented Nov 3, 2016 at 20:43

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.