基于索引vs ix的pandas删除行

2024-05-15 05:49:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图根据pandas的索引(而不是位置)删除它的dataframe行。

数据帧看起来像

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

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

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

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

那我查一下 df.ix[571]

那他妈的还在那儿!

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

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

我的问题是

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

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


Tags: and数据developerdataframepandasdfforindex
2条回答

您应该直接从索引中^{}所需的值:

df.drop(571, inplace=True)
df.index

是数据帧的索引。

df.index[571]

是索引的第571个元素。然后你把那东西掉了。你不想要位置,但你就是这么做的。

使用@John Zwinck的答案

相关问题 更多 >

    热门问题