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

With a multi-index we have to specify the column using a tuple in order to drop a specific column, or specify the level to drop all columns with that key on that index level.

Instead of saying drop column ‘c’ say drop (‘a’,’c’) as shown below:

df.drop(('a', 'c'), axis = 1, inplace = True)

Or specify the level as shown below

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

Let’s make a simple df to demonstrate on:

>>> cols = pd.MultiIndex.from_tuples([("a", "b"), ("a", "c"),
...                                   ("a", "f"), ('x', 'c'),('x', 'f')])
>>> df = pd.DataFrame([[1,3, 7, 21, 8], [2, 4, 9, 21, 8]], columns=cols)
>>> df
   a         x   
   b  c  f   c  f
0  1  3  7  21  8
1  2  4  9  21  8

Now here’s how to drop ‘c’ from ‘a’

>>> df.drop(('a', 'c'), axis = 1)
   a      x   
   b  f   c  f
0  1  7  21  8
1  2  9  21  8

With a three level index then include that key in the tuple to drop from the bottom level e.g. (‘a’,’c’,’k’)

With a single value as the index, like you did, it searches the top level index for a match by default and drops a match on that index or throws an error if the key is not in the index, like you found.

So in my example it would be fine to tell it to drop just ‘x’

>>> df.drop('x', axis = 1)
   a      
   b  c  f
0  1  3  7
1  2  4  9

To drop all columns with the second index ‘c’, then specify the level

>>> df.drop('c', axis = 1, level = 1)
   a     x
   b  f  f
0  1  7  8
1  2  9  8

Leave a Comment