Find below the code which is calculating the total number of subscribers, customers and other customers in each city from the excel files, and also calculating the average time of their trips in each city. Is there any way to simplify the If, elif statements inside the for loop in my code below?
new_file = {'Washington': './data/Washington-2016-Summary.csv',
'Chicago': './data/Chicago-2016-Summary.csv',
'NYC': './data/NYC-2016-Summary.csv'}
for city, filename in new_file.items():
with open (filename, 'r') as fil_1:
t_subscriber = 0
t_customers = 0
cnt_subscribers = 0
cnt_customers = 0
other_customers = 0
file_reader = csv.DictReader(fil_1)
for row in data_reader:
if row['user_type'] == 'Subscriber':
cnt_subscribers += 1
t_subscribers += float(row['duration'])
elif row['user_type'] == 'Customer':
cnt_customers += 1
t_customers += float(row['duration'])
elif row['user_type'] == '':
other_customers += 1
t_customers += float(row['duration'])
tripaverage_duration = (t_subscribers+t_customers)/60)/(cnt_subscribers+cnt_customers+other_customers)
tripaverage_subscribers = (t_subscribers/60)/cnt_subscribers
tripaverage_subscribers = (t_customers/60)/cnt_customers
print ('Average trip duration in', city,'-'
,tripaverage_duration,'minutes')
print ('Average trip duration for subscribers in', city,'-'
,tripaverage_subscribers,'minutes')
print ('Average trip duration for customers in', city,'-'
,tripaverage_subscribers,'minutes')
print ('\n')