0

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.

Eclipse
  • 3
  • 3
  • Your `NaN` may be a string. It's preferred to post a data sample as a copyable piece of code here so that others can quickly reproduce your problem (see [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391)). – AlexK Jan 25 '23 at 03:50

1 Answers1

0

Use a tuple of the levels:

df = df.dropna(axis=0, how="any", subset=[("Head 1", "Sub 1")])
Jason Baker
  • 3,170
  • 2
  • 12
  • 15