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

How to delete the elements with the date except 2020 from csv file with Python?

I have a csv file which has 3 columns. The first one is session_id, second one is item_id and last one is date. I am trying to delete the rows which does NOT have 2020 as date. Because I want to work on only 2020 dated data.

I did some research but I couldn’t find any clear information.

This is my code so far;

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

import numpy as np

dataset = pd.read_csv('purchases.csv')
dataset.info() #no null value, no missing data, nothing to drop

Can you help me ?

>Solution :

you can use:

#if your date column's type is string-object:
#df['date']=pd.to_datetime(df['date'])

df=df[df['your_date_column'].dt.year == 2020]
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