[英]Dropping all rows except ones with a certain criteria
I want to drop all rows that do not contain "PD-19-05-16" or "PD-19-06-01" in column "Document No."我想删除“文档编号”列中不包含“PD-19-05-16”或“PD-19-06-01”的所有行。 but after I ran the code it doesn't appear to make any changes.
但在我运行代码后,它似乎没有进行任何更改。
Any advice on how to properly do this or correct the code would be greatly appreciated.关于如何正确执行此操作或更正代码的任何建议将不胜感激。
I tried using df.drop but have been running into lots of problems.我尝试使用 df.drop 但遇到了很多问题。
df = df.drop(['Document No.'] != ['PD-19-05-16','PD-19-06-01'])
I expected this to work but got ValueError: Arrays were different lengths: 4533 vs 2我希望这能工作,但得到 ValueError: Arrays are different lengths: 4533 vs 2
try:尝试:
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)]
I don't think that's the correct use of drop tbf.我认为这不是 drop tbf 的正确用法。 I always find it easier to use
isin
to filter on a single column.我总是发现使用
isin
过滤单个列更容易。
the ~
acts as negatory operator ~
作为否定运算符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.