Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete second row of header in PANDAS

I have a dataframe in PANDAS which has two lines of headers.How could I remove the second line? For example, I have the following:

         AA  BB  CC  DD
         A   B   C   D
Index    
   1     1   2   3   4
   2     5   6   7   8
   3     9   1   2   3

and I would like to get something like this:

         AA  BB  CC  DD
Index    
   1     1   2   3   4
   2     5   6   7   8
   3     9   1   2   3

Thank you very much.

like image 598
km1234 Avatar asked Apr 10 '16 12:04

km1234


1 Answers

You can use droplevel with -1: last level:

df.columns = df.columns.droplevel(-1)
print df
       AA  BB  CC  DD
Index                
1       1   2   3   4
2       5   6   7   8
3       9   1   2   3

Or specify second level: 1:

df.columns = df.columns.droplevel(1)
print df
       AA  BB  CC  DD
Index                
1       1   2   3   4
2       5   6   7   8
3       9   1   2   3
like image 62
jezrael Avatar answered Sep 18 '22 19:09

jezrael