繁体   English   中英

删除除具有特定条件的行以外的所有行

[英]Dropping all rows except ones with a certain criteria

我想删除“文档编号”列中不包含“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 are 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过滤单个列更容易。

~作为否定运算符

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM