【发布时间】:2021-09-06 02:13:39
【问题描述】:
我的第一篇文章。我希望我能够正确地提出这个问题。 在下面的df中有需要根据多个条件删除的行。
所有行,其中“ID”存在(可能是一次或多次)并且都显示“confTyp” == “new” & “trType == “order” & Version == 1 表示这些是有效条目。
现在,如果“ID”不是唯一的,并且具有相同“ID”的行之一显示“confTyp”!=new 或“trTyp”!=“order”。需要删除具有相同“ID”的所有行。这也意味着必须删除带有假定正确的“confTyp”、“trTyp2”和“Version”的初始“ID”。
删除任何 != "new" 仍然会留下原始条目,然后也必须将其删除。
我已经以许多不同的方式尝试了 df.drop() 方法,但我远不是一个好的解决方案。有谁知道什么方法合适?
感谢您的帮助。
我有以下数据框:
| ID | confTyp | trType | Version |
|---|---|---|---|
| 100 | new | order | 1 |
| 101 | new | order | 1 |
| 102 | new | order | 1 |
| 103 | new | order | 1 |
| 104 | new | order | 1 |
| 105 | new | order | 1 |
| 106 | new | order | 1 |
| 107 | replace | manual | 1 |
| 106 | cancel | cancel | 2 |
| 106 | replace | manual | 1 |
| 105 | replace | replace | 2 |
| 104 | cancel | cancel | 2 |
| 108 | new | order | 1 |
目标是以下输出:
| ID | confTyp | trType | Version |
|---|---|---|---|
| 100 | new | order | 1 |
| 101 | new | order | 1 |
| 102 | new | order | 1 |
| 103 | new | order | 1 |
| 108 | new | order | 1 |
【问题讨论】:
-
使用
df[~df["ID"].duplicated(keep=False)]获取唯一ID。
标签: python pandas multiple-conditions drop