Home > Software design >  Drop certain rows in Pands
Drop certain rows in Pands

Time:05-18

I have a dataset (csv) with the column attribute date and the format of the attribute like this:

2019-10-18 00:00:00 00:00

Let's say I want to drop from the dataset all the rows for the years 2019 and 2020 and use the remaining for my model

How would I do that based on that format? I have tried something like this but I don't think it works...

index_names = dataset[ dataset['date'] == 2019 ].index
dataset.drop(index_names, inplace = True)

CodePudding user response:

df.drop(df.loc['startdatetime': 'endatetime'].index, inplace=True)

CodePudding user response:

Use .str.contains('^2019') to fetch all records with 2019, and add .astype(str) if the date is in a non-string format

index_names = dataset[ dataset['date'].astype(str).str.contains(f'^2019', case = False)].index
dataset.drop(index_names, inplace = True)
  • Related