Home > Net >  Can't Delete Row from Dataframe with MultiIndex Column
Can't Delete Row from Dataframe with MultiIndex Column

Time:01-26

I have created a DataFrame with a MultiIndex in the columns, which I want to create as an empty DataFrame and add columns and values iteratively. I want the structure to look like this (I may decide to change the row labels later, but for now I am using default row indices):

  Head 1 Head 1 Head 2 Head 2
  Sub 1  Sub 2  Sub 3  Sub 4
0
1
2

I have been successfully able to create the DataFrame with 1 column, but the first row has a value of NaN (I presume this is because it's created empty and fills the data with something upon declaration). I have tried to delete the row with NaN in it but nothing seems to be working. What is the correct syntax for dropping the row? To clarify I have this:

  Head 1
  Sub 1
0 NaN
1 foo
2 bar

And I want it to look like this:

  Head 1
  Sub 1
1 foo
2 bar

I have tried using both df.drop and df.drop_na, but the dataframe does not change.

CodePudding user response:

Use a tuple of the levels:

df = df.dropna(axis=0, how="any", subset=[("Head 1", "Sub 1")])
  • Related