Drop rows WHERE date is a certain condition Pandas
Question:
I have a dataset where I would like to remove all rows where the date is 4/1/2022 within a column in my dataset. The Date column is datetime64ns
Data
ID Date
AA 1/1/2022
BB 1/1/2022
CC 4/1/2022
Desired
ID Date
AA 1/1/2022
BB 1/1/2022
Doing
new = df[df['Date'].str.contains('4/1/2022')==False]
However, this is not a string, it is datetime. This is not removing the rows at all. I am still researching. Any suggestion is appreciated.
Answers:
You are on the right track. coerce the date to string and filter
df[~df['Date'].astype(str).str.contains('4/1/2022')]
Use boolean indexing:
df[df['Date'].ne('2022-04-01')]
Output:
ID Date
0 AA 2022-01-01
1 BB 2022-01-01
If for some reason you need to use drop
(e.g. to modify a DataFrame in place):
m = df['Date'].eq('2022-04-01')
df.drop(m[m].index, inplace=True)
multiple dates
df[~df['Date'].isin(['2022-04-01', '2022-04-03'])]
I have a dataset where I would like to remove all rows where the date is 4/1/2022 within a column in my dataset. The Date column is datetime64ns
Data
ID Date
AA 1/1/2022
BB 1/1/2022
CC 4/1/2022
Desired
ID Date
AA 1/1/2022
BB 1/1/2022
Doing
new = df[df['Date'].str.contains('4/1/2022')==False]
However, this is not a string, it is datetime. This is not removing the rows at all. I am still researching. Any suggestion is appreciated.
You are on the right track. coerce the date to string and filter
df[~df['Date'].astype(str).str.contains('4/1/2022')]
Use boolean indexing:
df[df['Date'].ne('2022-04-01')]
Output:
ID Date
0 AA 2022-01-01
1 BB 2022-01-01
If for some reason you need to use drop
(e.g. to modify a DataFrame in place):
m = df['Date'].eq('2022-04-01')
df.drop(m[m].index, inplace=True)
multiple dates
df[~df['Date'].isin(['2022-04-01', '2022-04-03'])]