I'm new to Python and could really use some help on this. I want to create a function to filter which files I want to open and which months and day specifically. That way, the users need to input which city(files) they want to analyze on which particular month or day. However, I want the user to be able to input something that is not case sensitive. For example, the user can input 'chicago'/'CHICAGO"/"ChIcAgO" and the it still give you the right output and not the error handling message. Here is the code I use:
def get_filters():
"""
Asks user to specify a city, month, and day to analyze.
Returns:
(str) city - name of the city to analyze
(str) month - name of the month to filter by, or "none" to apply no month filter
(str) day - name of the day of week to filter by, or "none" to apply no day filter
"""
print('Hello! Let\'s explore some US bikeshare data!')
# get user input for city (Chicago, New York, Washington). HINT: Use a while loop to handle invalid inputs
valid_cities = ['Chicago', 'New York', 'Washington']
city = valid_cities.index(input('Would you like to filter the data for Chicago, New York or Washington? \n')
while not city in valid_cities:
city = input('Selection error! Please choose a vaild city \n')
print("you chose %s." % city)
# get user input for month (all, January, February, ... , June)
valid_months = ['January', 'February', 'March', 'April', 'May', 'June', 'none']
month = input('Would like to filter data by month? Type e.g. "May" for month, or "none" for no month filter \n')
while not month in valid_months:
month = input('Selection error! Please enter a valid month or "none" for no month filter. \n')
print("you chose %s." % month)
# get user input for day of week (all, Monday, Tuesday, ... Sunday)
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'none']
day = input('Which day? Please type a day, e.g. "Monday", or type "none" for no day filter \n')
while not day in days:
day = input('Selection error! Please enter a valid day or "none" for no day filter. \n')
print("you chose %s." % day)
print('-'*40)
return city, month, day
def load_data(city, month, day):
"""
Loads data for the specified city and filters by month and day if applicable.
Args:
(str) city - name of the city to analyze
(str) month - name of the month to filter by, or "none" to apply no month filter
(str) day - name of the day of week to filter by, or "none" to apply no day filter
Returns:
df - Pandas DataFrame containing city data filtered by month and day
Start Time,End Time,Trip Duration,Start Station,End Station,User Type,Gender or "No gender data to share" if no data ,Birth Year
1423854,2017-06-23 15:09:32,2017-06-23 15:14:53,321,Wood St & Hubbard St,Damen Ave & Chicago Ave,Subscriber,Male,1992.0
"""
df = pd.read_csv(CITY_DATA[city])
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['month'] = df['Start Time'].dt.month
df['day_of_week'] = df['Start Time'].dt.weekday_name
if month != 'none':
# use the index of the months list to get the corresponding int
months = ['January', 'February', 'March', 'April', 'May', 'June']
month = months.index(month) + 1
df = df[df['month'] == month]
if day != 'none':
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
df = df[df['day_of_week'] == day.title()]
df = df[df['day_of_week'] == day]
return df