Python: Drop Column from DataFrame Multiindex

Viewed 68

I have the following DataFrame:

data_raw (201 x 600)
Column Level 0: ROE_1 ROE_2 Test_EQ_1
Column Level 1: A B C D E F G H I J .... (200 items)
Index Level 0: 1 2 3 4... (201 items)

I am trying to drop the Test_EQ_1 column.

After using:

data_raw = data_raw.drop("Test_EQ_1", axis=1, level=0)

The dataframe's dimension is reduced to 201x400 (as expected). All columns with Test_EQ_1 are gone (as expected).

However, when I use the following:

data_raw.columns.levels[0]

I obtain:

Index(['ROE_1', 'ROE_2', **'Test_EQ_1'**], dtype='object', name='Variable')

This is generating issues for later parts of my code, as Test_EQ_1 is gone from the dataframe, but still present when I check the columns.

Is there a way to "completely" delete Test_EQ_1?

1 Answers

Found an answer: Redefine the columns:

    multi = data_raw.columns.to_list()
    data_raw.columns = pd.MultiIndex.from_tuples(multi)
Related