I am trying to filter in Pandas for a selected period. (picture added) The start and the end date is to be entered in an input box.
"Day" is found in the index column, however the third line gives an error message.
df = pd.read_excel('prices.xlsx', index_col=0)
df.iloc[::-1]
filtered_date = df[(df['Day'] >= 'start_date') & (df['Day'] <= 'end_date')]
I get a "KeyError" message. I googled that key_error happens if a key is not available in a dictionary. I do not use a dictionary in my code, and I do not understand how to fix it. The key "Day" is indeed the first value of the first(index) row.
Thank you.
Look at the picture of your DataFrame:
Your code contains df['Day'], so you attempt to reference a regular column named Day. As regular column of this name does not exist, an exception is thrown.
There are 2 ways to cope with this:
Of course, put any valid date string instead of start_date and end_date.
And one more thing to consider: As Day is a matter of fact a column holding dates, it should have datetime type. So probably you should add parse_dates=[0] parameter to read_excel, to have this column converted from string to datetime.