Home > Blockchain >  Drop rows WHERE date is a certain condition Pandas
Drop rows WHERE date is a certain condition Pandas

Time:11-15

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.

CodePudding user response:

Drop all rows where the date is 4/1/2022.

df = df.drop(df['Date'] == '4/1/2022')

CodePudding user response:

You are on the right track. coerce the date to string and filter

df[~df['Date'].astype(str).str.contains('4/1/2022')]

CodePudding user response:

Use boolean indexing:

df[df['Date'].ne('2022-04-01')]

Output:

   ID       Date
0  AA 2022-01-01
1  BB 2022-01-01
  • Related