Apply groupby on multiple columns while taking aggregate in Python

Question:

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.

Asked By: Lynn

||

Answers:

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
Answered By: Naveed
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.