Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing group header after pandas aggregation

Suppose I have the following:

In [20]: df.groupby("dummy").agg({"returns": [np.mean, np.sum]})
Out[20]: 
        returns          
            sum      mean

dummy                    
1      0.285833  0.028583

How do I remove the "returns" header? So that I have a dataframe with columns 'sum' and 'mean'?

like image 857
italianfoot Avatar asked Jan 24 '26 07:01

italianfoot


1 Answers

Use -

df.columns = df.columns.droplevel(0)

To drop the top column level

like image 135
Ian Smith Avatar answered Jan 25 '26 20:01

Ian Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!