pandas根据索引vs ix删除行

Aer*_*rin 17 python dataframe pandas

我试图根据其索引(而不是位置)丢弃pandas数据帧行.

数据框看起来像

                      DO  
129518  a developer and   
20066   responsible for   
571     responsible for   
85629   responsible for   
5956    by helping them   
Run Code Online (Sandbox Code Playgroud)

(仅供参考:"DO"是列名)

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

df=df.drop(df.index[571])
Run Code Online (Sandbox Code Playgroud)

然后我检查df.ix [571]

那到底还有什么呢!

所以我认为"好吧,也许索引和ix是不同的!"

In [539]: df.index[571]
17002
Run Code Online (Sandbox Code Playgroud)

我的问题是

1)什么是指数?(与ix相比)

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

Joh*_*nck 30

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

df.drop(571, inplace=True)
Run Code Online (Sandbox Code Playgroud)


piR*_*red 7

df.index
Run Code Online (Sandbox Code Playgroud)

是数据帧的索引。

df.index[571]
Run Code Online (Sandbox Code Playgroud)

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

使用@John Zwinck 的回答