Home > Net >  Apply groupby on multiple columns while taking aggregate in Python
Apply groupby on multiple columns while taking aggregate in Python

Time:11-03

I wish to apply a groupby to multiple columns while running an aggregate function.

Data

country type    en  start   
Japan   aa      25  8/1/2022    
Japan   cc      1   9/1/2022    
US      bb      5   8/1/2022    
US      bb      5   8/1/2022    
                
            

Desired

country type    en  start    
Japan   aa      25  8/1/2022    
Japan   cc      1   9/1/2022    
US      bb      10  8/1/2022    
            

Doing

df.groupby(['country','type','date'])['en'].sum()

However, this is creating some blank rows. Any suggestion is appreciated.

CodePudding user response:

out=df.groupby(['country','type','start'], as_index=False).agg({'en': sum})
out
country     type    start   en
0   Japan   aa  8/1/2022    25
1   Japan   cc  9/1/2022    1
2   US      bb  8/1/2022    10
  • Related