Thursday, 14 December 2017

python - Is there any way to simplify the If, elif statements inside the for loop in my code below?

itemprop="text">

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

class="post-text" itemprop="text">
class="normal">Answer



I
recommend href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html"
rel="nofollow noreferrer">Pandas dataframes for something like this. You
can easily subset dataframes based on values in another column, and sum the values,
count the numbers, etc. Here's an example of how you could apply this to your
problem:




import pandas
as pd
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():

data = pd.read_csv(filename)

tripaverage_duration = data.values.mean()['duration']
tripaverage_subscribers
= data[data['user_type']=='Subscriber'].values.mean()['duration']


tripaverage_customers =
data[data['user_type']=='Customer'].values.mean()['duration']


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



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...