'Drop certain rows in Pands
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)
Solution 1:[1]
df.drop(df.loc['startdatetime': 'endatetime'].index, inplace=True)
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DontDownvote |
| Solution 2 | Rafael M R de Rezende |
