我正在嘗試查看是否可以找到當年發生的重復記錄
我知道如何找到只是一年的時間,但日期范圍并找到重復項并洗掉重復項似乎有點困難。我是初學者,所以有人可以幫忙嗎?
uj5u.com熱心網友回復:
您可以使用:
out = df[~df.loc[df['Date'].str.contains('2021')].duplicated(keep=False)
.reindex(df.index, fill_value=False)]
print(out)
# Output
Date Transaction
0 31/12/2020 PURCHASES
1 31/12/2020 Sales
2 31/12/2020 Sales
uj5u.com熱心網友回復:
使用GroupBy.cumcount按年和交易分組
year_s = pd.to_datetime(df['Date']).dt.year
df[~(df.groupby([year_s, 'Transaction']).cumcount().gt(0) & year_s.eq(2021))]
Date Transaction
0 31/12/2020 PURCHASES
1 31/12/2020 Sales
2 31/12/2020 Sales
3 04/01/2021 PURCHASES
4 04/01/2021 Sales
如果您不想按年檢查重復:
df[~(df['Transaction'].duplicated() & year_s.eq(2021))]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/443052.html
標籤:Python 熊猫 jupyter-笔记本
上一篇:ro如何讀取大量csv索引檔案
