Pandas Groupby and Sum Only One Column

后端 未结 2 1129
孤独总比滥情好
孤独总比滥情好 2020-11-27 05:33

So I have a dataframe, df1, that looks like the following:

       A      B      C
1     foo    12    California
2     foo    22    California
3     bar    8          


        
相关标签:
2条回答
  • 2020-11-27 05:57

    If you don't care what's in your column C and just want the nth value, you could just do this:

    df.groupby('A').agg({'B' : 'sum',
                         'C' : lambda x: x.iloc[n]})
    
    0 讨论(0)
  • 2020-11-27 06:13

    The only way to do this would be to include C in your groupby (the groupby function can accept a list).

    Give this a try:

    df.groupby(['A','C'])['B'].sum()
    

    One other thing to note, if you need to work with df after the aggregation you can also use the as_index=False option to return a dataframe object. This one gave me problems when I was first working with Pandas. Example:

    df.groupby(['A','C'], as_index=False)['B'].sum()
    
    0 讨论(0)
提交回复
热议问题