Pandas: drop a level from a multi-level column index?
Another way to drop the index is to use a list comprehension:
df.columns = [col 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.