Pandas - dataframe groupby - how to get sum of multiple columns

前端 未结 5 610
别跟我提以往
别跟我提以往 2020-12-05 02:34

This should be an easy one, but somehow I couldn\'t find a solution that works.

I have a pandas dataframe which looks like this:

index col1   col2            


        
5条回答
  •  渐次进展
    2020-12-05 03:27

    The above answer didn't work for me.

    df_new = df.groupby(['col1', 'col2']).sum()[["col3", "col4"]]
    

    I was grouping by single group by and sum columns.

    Here is the one worked for me.

    D1.groupby(['col1'])['col2'].sum() << The sum at the end not the middle.
    

提交回复
热议问题