9

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.

km1234
  • 2,127
  • 4
  • 18
  • 21

1 Answers1

17

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252