Pandas group by and sum two columns

后端 未结 3 470
鱼传尺愫
鱼传尺愫 2021-01-12 09:43

Beginner question. This seems like it should be a straightforward operation, but I can\'t figure it out from reading the docs.

I have a df with this structure:

相关标签:
3条回答
  • 2021-01-12 10:23

    A variation on the .agg() function; provides the ability to (1) persist type DataFrame, (2) apply averages, counts, summations, etc. and (3) enables groupby on multiple columns while maintaining legibility.

    df.groupby(['att1', 'att2']).agg({'att1': "count", 'att3': "sum",'att4': 'mean'})
    

    using your values...

    df.groupby(['integer_id']).agg({'int_field_1': "sum", 'int_field_2': "sum" })
    
    0 讨论(0)
  • 2021-01-12 10:37

    You just need to call sum on a groupby object:

    df.groupby('integer_id').sum()
    

    See the docs for further examples

    0 讨论(0)
  • 2021-01-12 10:37

    You can do it

    data.groupby(by=['account_ID'])['purchases'].sum()
    
    0 讨论(0)
提交回复
热议问题