从数据帧中删除一个元素(错误项不存在)[重复] 。

问题描述 投票:0回答:1

我在用 df=df.drop(index='historical book') 删除所有包含 historical book.

第一行是标题

id           name book              subject                       amount
1            name1                  historical book               6
2            name2                  literature book               9
3            name3                  historical book               4

我收到一个错误信息,说这是 not found in axis. 你知道为什么吗?

python pandas numpy jupyter-notebook data-visualization
1个回答
1
投票

这样做就可以了。

print(df[~df['subject'].str.contains('historical book')])

   id name book          subject  amount
1   2     name2  literature book       9
© www.soinside.com 2019 - 2024. All rights reserved.