Python 熊猫根据索引与 ix 删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39802076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:43:41  来源:igfitidea点击:

pandas drop row based on index vs ix

pythonpandasdataframe

提问by Aerin

I'm trying to drop pandas dataframe row based on its index (not location).

我正在尝试根据其索引(而不是位置)删除 pandas 数据框行。

The data frame looks like

数据框看起来像

                      DO  
129518  a developer and   
20066   responsible for   
571     responsible for   
85629   responsible for   
5956    by helping them   

(FYI: "DO" is a column name)

(仅供参考:“DO”是列名)

I want to delete the row where its index is 571 so I did:

我想删除索引为 571 的行,所以我做了:

df=df.drop(df.index[571])

then I check df.ix[571]

然后我检查 df.ix[571]

then what the hell it's still there!

那他妈的怎么还在那里!

So I thought "ok, maybe index and ix are different!"

所以我想“好吧,也许 index 和 ix 是不同的!”

In [539]: df.index[571]
17002

My question is

我的问题是

1) What is index? (compared to ix)

1)什么是索引?(与ix相比)

2) How do I delete the index row 571 using ix?

2) 如何使用 ix 删除索引行 571?

回答by John Zwinck

You should dropthe desired value from the index directly:

您应该drop直接从索引中获得所需的值:

df.drop(571, inplace=True)

回答by piRSquared

df.index

Is the index of the dataframe.

是数据帧的索引。

df.index[571]

Is the 571st element of the index. Then you dropped whatever that was. You didn't want positional but that's what you did.

是索引的第 571 个元素。然后你放弃了任何东西。你不想要位置,但这就是你所做的。

Use @John Zwinck's answer

使用@John Zwinck 的回答