pandasdataframemultiple-columnsmulti-level

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


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)'


Solution

  • Solved:

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