Home > OS >  Drop specific date in dataframe
Drop specific date in dataframe

Time:01-20

As titile I want to drop specific date in dataframe
I want to discard these specific days throughout the year

array(['2020-01-02', '2020-01-03', '2020-01-04',
   '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',
   '2020-01-09', '2020-01-11', '2020-01-12','2020-01-13'
   '2020-01-14', '2020-01-15', '2020-01-16'])

so I use dates_list = [dt.datetime.strptime(date, "%Y-%m-%d").date() for date in day]and also year['day'] = pd.to_datetime(year['day'])make str to datetime

then dropyear.drop(dates_list)

But it can't work where am i doing wrong??

CodePudding user response:

To achieve this, you can use the isin() functionality in python.

df = df[~df['date'].isin(dates_list)]

This will basically remove all the rows where the 'date' column contains a value which is in dates_list.

CodePudding user response:

When using drop you're supposed to target an index or a column label. So either set your column to index and drop it:

df.set_index('year').drop(dates_list)

Or loc what you want directly:

df.loc[~df.year.isin(dates_list)]
  •  Tags:  
  • Related