简体   繁体   中英

Can't Delete Row from Dataframe with MultiIndex Column

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.

Use a tuple of the levels:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM