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

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


You can use MultiIndex.droplevel:

>>> cols = pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")])>>> df = pd.DataFrame([[1,2], [3,4]], columns=cols)>>> df   a      b  c0  1  21  3  4[2 rows x 2 columns]>>> df.columns = df.columns.droplevel()>>> df   b  c0  1  21  3  4[2 rows x 2 columns]


Another way to drop the index is to use a list comprehension:

df.columns = [col[1] for col in df.columns]   b  c0  1  21  3  4

This strategy is also useful if you want to combine the names from both levels like in the example below where the bottom level contains two 'y's:

cols = pd.MultiIndex.from_tuples([("A", "x"), ("A", "y"), ("B", "y")])df = pd.DataFrame([[1,2, 8 ], [3,4, 9]], columns=cols)   A     B   x  y  y0  1  2  81  3  4  9

Dropping the top level would leave two columns with the index 'y'. That can be avoided by joining the names with the list comprehension.

df.columns = ['_'.join(col) for col in df.columns]    A_x A_y B_y0   1   2   81   3   4   9

That's a problem I had after doing a groupby and it took a while to find this other question that solved it. I adapted that solution to the specific case here.


As of Pandas 0.24.0, we can now use DataFrame.droplevel():

cols = pd.MultiIndex.from_tuples([("a", "b"), ("a", "c")])df = pd.DataFrame([[1,2], [3,4]], columns=cols)df.droplevel(0, axis=1) #   b  c#0  1  2#1  3  4

This is very useful if you want to keep your DataFrame method-chain rolling.