This is a follow up question (?) to a post I made yesterday: Python pandas df.loc not being found in a table
I'm trying to I'm trying to find a certain row and column in a given .csv file with pandas. Here is a snapshot of the table: Table
The table goes from 1/1/2015 to 12/31/2017. I've located the specific column and date I want to use from the csv file, and printing it so I can see if it's working properly. This is the code I have thus far:
months = {'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6,
'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12}
month = str(input('Enter a month: '))
year = str(input('Enter a year: '))
if not (2015 <= float(year) <= 2017):
print('Enter a year bewteen 2015 and 2017, both included and try again')
exit()
day = 1
df1 = df.set_index(['Date'])
if (month == 'January' or 'March' or 'May' or 'July' or 'August' or 'October' or 'December'):
while day < 32:
find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
print(find)
day += 1
elif (month == 'April' or 'June' or 'September' or 'November'):
while day < 31:
find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
print(find)
day += 1
elif (month == 'February'):
if year == '2016':
while day < 29:
find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
print(find)
day += 1
else:
while day < 28:
find = df1.loc[[str(months[month]) + '/' + str(day) + '/' + str(year)], ['Temp Low']]
print(find)
day += 1
This code is working correctly for months with 31 days, but breaks for any other month. For example if I enter "June" for the month (no quotations), the code works until it reaches day 30, then tries to look for day 31. Why is this happening? It seems to be searching for day 31 in the date column even though I've restricted it to be less than 31.
Picture of error code: Error