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

Time:11-23

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;

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 ?

CodePudding user response:

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]
  • Related