Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Drop rows WHERE date is a certain condition Pandas

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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'])]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading