Search code examples
pythonpandasdataframeindexingpandas-groupby

Drop rows from dataframe based on date index gives "KeyError: "[''column_name'] not found in axis""


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'])

Solution

  • 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)