我想删除“文档编号”列中不包含“PD-19-05-16”或“PD-19-06-01”的所有行。但在我运行代码后,它似乎没有进行任何更改。
任何有关如何正确执行此操作或更正代码的建议将不胜感激。
我尝试使用 df.drop 但遇到了很多问题。
df = df.drop(['Document No.'] != ['PD-19-05-16','PD-19-06-01'])
我预计这会起作用,但得到了 ValueError: Arrays were different lengths: 4533 vs 2
最佳答案
尝试:
rows_to_keep = ["PD-19-05-16","PD-19-06-01"]
df.loc[df['Document No.'].isin(rows_to_keep)]
#or if you need the inverse
df.loc[~df['Document No.'].isin(rows_to_keep)]
我认为这不是 drop tbf 的正确用法。我总是发现使用 isin
来过滤单个列更容易。
~
充当否定运算符
关于python - 删除除具有特定条件的行之外的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57313493/