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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With