我有一個資料集,我想在其中洗掉資料集中一列中日期為 4/1/2022 的所有行。日期列是 datetime64ns
資料
ID Date
AA 1/1/2022
BB 1/1/2022
CC 4/1/2022
想要的
ID Date
AA 1/1/2022
BB 1/1/2022
正在做
new = df[df['Date'].str.contains('4/1/2022')==False]
但是,這不是字串,而是日期時間。這根本沒有洗掉行。我還在研究。任何建議表示贊賞。
uj5u.com熱心網友回復:
使用布爾索引:
df[df['Date'].ne('2022-04-01')]
輸出:
ID Date
0 AA 2022-01-01
1 BB 2022-01-01
如果出于某種原因您需要使用drop(例如,就地修改 DataFrame):
m = df['Date'].eq('2022-04-01')
df.drop(m[m].index, inplace=True)
多個日期
df[~df['Date'].isin(['2022-04-01', '2022-04-03'])]
uj5u.com熱心網友回復:
你走在正確的軌道上。將日期強制轉換為字串并進行過濾
df[~df['Date'].astype(str).str.contains('4/1/2022')]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/533778.html
