伙計們!
我遇到了與從一列中洗掉重復項與另一列相比的問題。有兩列的 Excel 檔案。就好像是:
| 第一列 | 第二列 |
|---|---|
| 字串 1 | 字串 2 |
| 字串 3 | 字串 4 |
| 字串 5 | 字串 6 |
| 字串 7 | 字串 3 |
| ... | ... |
| 字串 N | 鈉 |
因此,第一列中的一些值在第二列中重復。我想從第一個值中洗掉這些值。
我嘗試使用drop_duplicates(keep="last"),但效果不佳。
我已經學會了如何通過 pandas 閱讀 excel 并列印它,但是洗掉重復項讓我陷入了困境。然后我想收到“干凈”的第一列并將其寫入新檔案,但我想我可以自己完成。
這是我的代碼:
import pandas as pd
file_location = r"PATH/file.xlsx"
file = pd.read_excel(file_location)
file = file.drop_duplicates(keep="last")
print(file)
我希望你能幫助我或推動正確的想法!
uj5u.com熱心網友回復:
這應該有效:
df.loc[~df['first_column'].isin(df['second_column'].tolist())]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428698.html
上一篇:匯入CSV時如何解決以下錯誤
