[英]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.我创建了一个 DataFrame,在列中有一个 MultiIndex,我想创建一个空的 DataFrame 并迭代地添加列和值。 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).我已经成功地创建了具有 1 列的 DataFrame,但第一行的值为 NaN(我认为这是因为它创建为空并在声明时用某些东西填充数据)。 I have tried to delete the row with NaN in it but nothing seems to be working.
我试图删除其中包含 NaN 的行,但似乎没有任何效果。 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.我试过同时使用 df.drop 和 df.drop_na,但 dataframe 没有改变。
Use a tuple of the levels:使用级别元组:
df = df.dropna(axis=0, how="any", subset=[("Head 1", "Sub 1")])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.