I'm trying to create a new dataframe excluding the rows containing two dates, my date column is the index.
When I use DF2 = DF.drop(DF.loc['03/01/2018':'03/02/2018'])
I get the error
KeyError: "['Column_name1' 'Column_name2'] not found in axis"
I've tried adding axis = 0 to specify that I want to drop rows, but still get the same error DF2 = DF.drop(DF.loc['03/01/2018':'03/02/2018'], axis = 0)
If I try and print the 'loc' it returns the rows as expected print(DF.loc['03/01/2018':'03/02/2018'])
CodePudding user response:
Since you say your date is the index, when you use DF.loc['03/01/2018':'03/02/2018']
, you are locating the rows that are between 03/01/2018
and 03/02/2018
.
pandas.DataFrame.drop
accepts index or column label, by default it accepts index label. You should use
DF2 = DF.drop(['03/01/2018', '03/02/2018'])
# or
DF2 = DF[~DF.index.isin('03/01/2018', '03/02/2018')]
CodePudding user response:
your statement just need .index
at the end. to do the slicing like this you need loc, but drop
wants index
as input.
DF2 = DF.drop(DF.loc['03/01/2018':'03/02/2018'].index)
If this doesn't work, then you should check the format of index (needs to be string the way you try to access it)
If index is in datetime.date
format you could do it like this:
DF2 = DF.drop(DF.loc[dateimte.date(2018,3,1):datetime.date(2018,3,2)].index)