Python Keep other columns when using sum() with groupby

前端 未结 3 817
遥遥无期
遥遥无期 2020-12-15 06:28

I have a pandas dataframe below:

    df

    name    value1    value2  otherstuff1 otherstuff2 
0   Jack       1         1       1.19        2.39     
1   Ja         


        
相关标签:
3条回答
  • 2020-12-15 06:43

    You should specify what pandas must do with the other columns. In your case, I think you want to keep one row, regardless of its position within the group.

    This could be done with agg on a group. agg accepts a parameter that specifies what operation should be performed for each column.

    df.groupby(['name'], as_index=False).agg({'value1': 'sum', 'value2': 'sum', 'otherstuff1': 'first', 'otherstuff2': 'first'})
    
    0 讨论(0)
  • 2020-12-15 06:48

    Something like ?(Assuming you have same otherstuff1 and otherstuff2 under the same name )

    df.groupby(['name','otherstuff1','otherstuff2'],as_index=False).sum()
    Out[121]: 
       name  otherstuff1  otherstuff2  value1  value2
    0  Jack         1.19         2.39       2       3
    1  Luke         1.08         1.08       1       1
    2  Mark         3.45         3.45       0       1
    
    0 讨论(0)
  • 2020-12-15 07:04

    The key in the answer above is actually the as_index=False, otherwise all the columns in the list get used in the index.

    p_summ = p.groupby( attributes_list, as_index=False ).agg( {'AMT':sum })
    
    0 讨论(0)
提交回复
热议问题