km1234
km1234

Reputation: 2319

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.

Upvotes: 9

Views: 16565

Answers (1)

jezrael
jezrael

Reputation: 862511

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

Upvotes: 18

Related Questions