Python Pandas: drop a column from a multi-level column index?

Boosted_d16 picture Boosted_d16 · Aug 5, 2014 · Viewed 21.7k times · Source

I have a multi level column table like this:

    a
   ---+---+---
    b | c | f
--+---+---+---
0 | 1 | 2 | 7
1 | 3 | 4 | 9

How can I drop column "c" by name? to look like this:

    a
   ---+---
    b | f
--+---+---
0 | 1 | 7
1 | 3 | 9

I tried this:

del df['c']

but I get the following error, which makes sense:

KeyError: 'Key length (1) was greater than MultiIndex lexsort depth (0)'

Answer

Boosted_d16 picture Boosted_d16 · Aug 5, 2014

Solved:

df.drop('c', axis=1, level=1)