[英]Remove when 2 columns are duplicated, but keep based on value of a third column (pandas)
我正在尋找一種方法來刪除在條形碼和產品編號上重復的所有行,但在它們是最新輸入時保留這些重復的行。 下面的例子:
我有的:
輸入 ID | 條碼 | 產品編號 |
---|---|---|
001 | 225 | 111 |
001 | 225 | 111 |
001 | 225 | 111 |
002 | 225 | 111 |
002 | 225 | 111 |
002 | 225 | 111 |
002 | 225 | 111 |
003 | 226 | 222 |
003 | 226 | 222 |
004 | 226 | 222 |
004 | 226 | 222 |
005 | 227 | 222 |
005 | 227 | 222 |
006 | 227 | 222 |
006 | 227 | 222 |
Output:
輸入 ID | 條碼 | 產品編號 |
---|---|---|
002 | 225 | 111 |
002 | 225 | 111 |
002 | 225 | 111 |
002 | 225 | 111 |
004 | 226 | 222 |
004 | 226 | 222 |
006 | 227 | 222 |
006 | 227 | 222 |
您可以看到條形碼和產品編號的位置。 除了最高的輸入 ID 行之外,其他所有行都相同,現在已刪除,只留下具有最新輸入的重復項。
謝謝奧利
您可以運行duplicated
以識別最后一個副本並使用groupby
+ transform('any')
擴展每個組的選擇:
df[((~df[['Product No.', 'Barcode']].duplicated(keep='last'))
.groupby(df['Input ID']).transform('any'))]
output:
Input ID Barcode Product No.
3 2 225 111
4 2 225 111
5 2 225 111
6 2 225 111
9 4 226 222
10 4 226 222
13 6 227 222
14 6 227 222
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.