Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a column from MultiIndex Pandas dataframe

I am trying to delete the "std" sub-column from a multiindex DataFrame.

                                    attribute                           attribute2  \
                                        test1            std           test2   
d         count       type                                                  
r1         10          rx      0.559 (0.0)    0.559 (0.0)    0.568 (0.0)   
                     sth1      0.653 (0.004)  0.653 (0.004)  0.679 (0.002)   
                     sth2      0.584 (0.002)  0.584 (0.002)  0.586 (0.003)   
                     sth3      0.651 (0.005)  0.651 (0.005)  0.676 (0

I can't seem to get my head around the pandas.MultiIndex.drop function.

The resulting dataframe should thus have only two mean columns, no "std"

How can this be done?

like image 944
sdgaw erzswer Avatar asked Nov 16 '25 08:11

sdgaw erzswer


1 Answers

I think better is for remove all columns with std is second level of MultiIndex use drop with parameter level=1:

print (df)
              attribute          attribute2          attribute3         
                  test1      std      test2      std      test3      std
d  count type                                                           
r1 10    rx       0.559    (0.0)      0.559    (0.0)      0.568    (0.0)
         sth1     0.653  (0.004)      0.653  (0.004)      0.679  (0.002)
         sth2     0.584  (0.002)      0.584  (0.002)      0.586  (0.003)
         sth3     0.651  (0.005)      0.651  (0.005)      0.676      (0)

df = df.drop('std', axis=1, level=1)
print (df)
              attribute attribute2 attribute3
                  test1      test2      test3
d  count type                                
r1 10    rx       0.559      0.559      0.568
         sth1     0.653      0.653      0.679
         sth2     0.584      0.584      0.586
         sth3     0.651      0.651      0.676
like image 119
jezrael Avatar answered Nov 17 '25 20:11

jezrael