forked from hassio-addons/repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbikeshare.py.txt
353 lines (285 loc) · 13.1 KB
/
bikeshare.py.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import time
import pandas as pd
import numpy as np
import datetime as dt
import click
CITY_DATA = {'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv'}
months = ('january', 'february', 'march', 'april', 'may', 'june')
weekdays = ('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday',
'saturday')
def choice(prompt, choices=('y', 'n')):
"""Return a valid input from the user given an array of possible answers.
"""
while True:
choice = input(prompt).lower().strip()
# terminate the program if the input is end
if choice == 'end':
raise SystemExit
# triggers if the input has only one name
elif ',' not in choice:
if choice in choices:
break
# triggers if the input has more than one name
elif ',' in choice:
choice = [i.strip().lower() for i in choice.split(',')]
if list(filter(lambda x: x in choices, choice)) == choice:
break
prompt = ("\nSomething is not right. Please mind the formatting and "
"be sure to enter a valid option:\n>")
return choice
def get_filters():
"""Ask user to specify city(ies) and filters, month(s) and weekday(s).
Returns:
(str) city -name of the city(ies) to analyze
(str) month -name of the month(s) to filter
(str) day -name of the day(s) of week to filter
"""
print("\n\nLet's explore some US bikeshare data!\n")
print("Type end at any time if you would like to exit the program.\n")
while True:
city = choice("\nFor what city(ies) do you want do select data, "
"New York City, Chicago or Washington? Use commas "
"to list the names.\n>", CITY_DATA.keys())
month = choice("\nFrom January to June, for what month(s) do you "
"want do filter data? Use commas to list the names.\n>",
months)
day = choice("\nFor what weekday(s) do you want do filter bikeshare "
"data? Use commas to list the names.\n>", weekdays)
# confirm the user input
confirmation = choice("\nPlease confirm that you would like to apply "
"the following filter(s) to the bikeshare data."
"\n\n City(ies): {}\n Month(s): {}\n Weekday(s)"
": {}\n\n [y] Yes\n [n] No\n\n>"
.format(city, month, day))
if confirmation == 'y':
break
else:
print("\nLet's try this again!")
print('-'*40)
return city, month, day
def load_data(city, month, day):
"""Load data for the specified filters of city(ies), month(s) and
day(s) whenever applicable.
Args:
(str) city - name of the city(ies) to analyze
(str) month - name of the month(s) to filter
(str) day - name of the day(s) of week to filter
Returns:
df - Pandas DataFrame containing filtered data
"""
print("\nThe program is loading the data for the filters of your choice.")
start_time = time.time()
# filter the data according to the selected city filters
if isinstance(city, list):
df = pd.concat(map(lambda city: pd.read_csv(CITY_DATA[city]), city),
sort=True)
# reorganize DataFrame columns after a city concat
try:
df = df.reindex(columns=['Unnamed: 0', 'Start Time', 'End Time',
'Trip Duration', 'Start Station',
'End Station', 'User Type', 'Gender',
'Birth Year'])
except:
pass
else:
df = pd.read_csv(CITY_DATA[city])
# create columns to display statistics
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['Month'] = df['Start Time'].dt.month
df['Weekday'] = df['Start Time'].dt.weekday_name
df['Start Hour'] = df['Start Time'].dt.hour
# filter the data according to month and weekday into two new DataFrames
if isinstance(month, list):
df = pd.concat(map(lambda month: df[df['Month'] ==
(months.index(month)+1)], month))
else:
df = df[df['Month'] == (months.index(month)+1)]
if isinstance(day, list):
df = pd.concat(map(lambda day: df[df['Weekday'] ==
(day.title())], day))
else:
df = df[df['Weekday'] == day.title()]
print("\nThis took {} seconds.".format((time.time() - start_time)))
print('-'*40)
return df
def time_stats(df):
"""Display statistics on the most frequent times of travel."""
print('\nDisplaying the statistics on the most frequent times of '
'travel...\n')
start_time = time.time()
# display the most common month
most_common_month = df['Month'].mode()[0]
print('For the selected filter, the month with the most travels is: ' +
str(months[most_common_month-1]).title() + '.')
# display the most common day of week
most_common_day = df['Weekday'].mode()[0]
print('For the selected filter, the most common day of the week is: ' +
str(most_common_day) + '.')
# display the most common start hour
most_common_hour = df['Start Hour'].mode()[0]
print('For the selected filter, the most common start hour is: ' +
str(most_common_hour) + '.')
print("\nThis took {} seconds.".format((time.time() - start_time)))
print('-'*40)
def station_stats(df):
"""Display statistics on the most popular stations and trip."""
print('\nCalculating The Most Popular Stations and Trip...\n')
start_time = time.time()
# display most commonly used start station
most_common_start_station = str(df['Start Station'].mode()[0])
print("For the selected filters, the most common start station is: " +
most_common_start_station)
# display most commonly used end station
most_common_end_station = str(df['End Station'].mode()[0])
print("For the selected filters, the most common start end is: " +
most_common_end_station)
# display most frequent combination of start station and
# end station trip
df['Start-End Combination'] = (df['Start Station'] + ' - ' +
df['End Station'])
most_common_start_end_combination = str(df['Start-End Combination']
.mode()[0])
print("For the selected filters, the most common start-end combination "
"of stations is: " + most_common_start_end_combination)
print("\nThis took {} seconds.".format((time.time() - start_time)))
print('-'*40)
def trip_duration_stats(df):
"""Display statistics on the total and average trip duration."""
print('\nCalculating Trip Duration...\n')
start_time = time.time()
# display total travel time
total_travel_time = df['Trip Duration'].sum()
total_travel_time = (str(int(total_travel_time//86400)) +
'd ' +
str(int((total_travel_time % 86400)//3600)) +
'h ' +
str(int(((total_travel_time % 86400) % 3600)//60)) +
'm ' +
str(int(((total_travel_time % 86400) % 3600) % 60)) +
's')
print('For the selected filters, the total travel time is : ' +
total_travel_time + '.')
# display mean travel time
mean_travel_time = df['Trip Duration'].mean()
mean_travel_time = (str(int(mean_travel_time//60)) + 'm ' +
str(int(mean_travel_time % 60)) + 's')
print("For the selected filters, the mean travel time is : " +
mean_travel_time + ".")
print("\nThis took {} seconds.".format((time.time() - start_time)))
print('-'*40)
def user_stats(df, city):
"""Display statistics on bikeshare users."""
print('\nCalculating User Stats...\n')
start_time = time.time()
# Display counts of user types
user_types = df['User Type'].value_counts().to_string()
print("Distribution for user types:")
print(user_types)
# Display counts of gender
try:
gender_distribution = df['Gender'].value_counts().to_string()
print("\nDistribution for each gender:")
print(gender_distribution)
except KeyError:
print("We're sorry! There is no data of user genders for {}."
.format(city.title()))
# Display earliest, most recent, and most common year of birth
try:
earliest_birth_year = str(int(df['Birth Year'].min()))
print("\nFor the selected filter, the oldest person to ride one "
"bike was born in: " + earliest_birth_year)
most_recent_birth_year = str(int(df['Birth Year'].max()))
print("For the selected filter, the youngest person to ride one "
"bike was born in: " + most_recent_birth_year)
most_common_birth_year = str(int(df['Birth Year'].mode()[0]))
print("For the selected filter, the most common birth year amongst "
"riders is: " + most_common_birth_year)
except:
print("We're sorry! There is no data of birth year for {}."
.format(city.title()))
print("\nThis took {} seconds.".format((time.time() - start_time)))
print('-'*40)
def raw_data(df, mark_place):
"""Display 5 line of sorted raw data each time."""
print("\nYou opted to view raw data.")
# this variable holds where the user last stopped
if mark_place > 0:
last_place = choice("\nWould you like to continue from where you "
"stopped last time? \n [y] Yes\n [n] No\n\n>")
if last_place == 'n':
mark_place = 0
# sort data by column
if mark_place == 0:
sort_df = choice("\nHow would you like to sort the way the data is "
"displayed in the dataframe? Hit Enter to view "
"unsorted.\n \n [st] Start Time\n [et] End Time\n "
"[td] Trip Duration\n [ss] Start Station\n "
"[es] End Station\n\n>",
('st', 'et', 'td', 'ss', 'es', ''))
asc_or_desc = choice("\nWould you like it to be sorted ascending or "
"descending? \n [a] Ascending\n [d] Descending"
"\n\n>",
('a', 'd'))
if asc_or_desc == 'a':
asc_or_desc = True
elif asc_or_desc == 'd':
asc_or_desc = False
if sort_df == 'st':
df = df.sort_values(['Start Time'], ascending=asc_or_desc)
elif sort_df == 'et':
df = df.sort_values(['End Time'], ascending=asc_or_desc)
elif sort_df == 'td':
df = df.sort_values(['Trip Duration'], ascending=asc_or_desc)
elif sort_df == 'ss':
df = df.sort_values(['Start Station'], ascending=asc_or_desc)
elif sort_df == 'es':
df = df.sort_values(['End Station'], ascending=asc_or_desc)
elif sort_df == '':
pass
# each loop displays 5 lines of raw data
while True:
for i in range(mark_place, len(df.index)):
print("\n")
print(df.iloc[mark_place:mark_place+5].to_string())
print("\n")
mark_place += 5
if choice("Do you want to keep printing raw data?"
"\n\n[y]Yes\n[n]No\n\n>") == 'y':
continue
else:
break
break
return mark_place
def main():
while True:
click.clear()
city, month, day = get_filters()
df = load_data(city, month, day)
mark_place = 0
while True:
select_data = choice("\nPlease select the information you would "
"like to obtain.\n\n [ts] Time Stats\n [ss] "
"Station Stats\n [tds] Trip Duration Stats\n "
"[us] User Stats\n [rd] Display Raw Data\n "
"[r] Restart\n\n>",
('ts', 'ss', 'tds', 'us', 'rd', 'r'))
click.clear()
if select_data == 'ts':
time_stats(df)
elif select_data == 'ss':
station_stats(df)
elif select_data == 'tds':
trip_duration_stats(df)
elif select_data == 'us':
user_stats(df, city)
elif select_data == 'rd':
mark_place = raw_data(df, mark_place)
elif select_data == 'r':
break
restart = choice("\nWould you like to restart?\n\n[y]Yes\n[n]No\n\n>")
if restart.lower() != 'y':
break
if __name__ == "__main__":
main()