forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bikeshare.py
318 lines (254 loc) · 9.83 KB
/
bikeshare.py
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
import time
import pandas as pd
# import numpy as np
CITY_DATA = {
"chicago": "chicago.csv",
"new york city": "new_york_city.csv",
"washington": "washington.csv",
}
MONTHS = ["january", "february", "march", "april", "may", "june"]
DAYS = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
def get_input(input_label, help_label, input_options):
"""
Helper function used inside 'get_filters' function.
Args:
(str) input_label: Message to be shown to the user for the filter input.
(str) help_label: Message sent to the user in case of wrong input.
(dict) input_options: Dictionary of input options to be chosen.
Returns:
(str) inpt: input option according to the user input.
"""
while True:
inpt = input(input_label).lower()
if inpt == "help":
print(help_label)
elif inpt in input_options:
break
else:
print("Sorry, wrong input.\n" + help_label)
return inpt
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 "all" to apply no month filter
(str) day: Name of the day of week to filter by, or "all" to apply no day filter
"""
print("Hello! Let's explore some US bikeshare data!")
# Get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
city = get_input(
"Please enter city name or [help]: ",
"Possible values are [chicago], [new york city], [washington] or [all].",
CITY_DATA,
)
# Get user input for month (all, january, february, ... , june)
month = get_input(
"Please enter month or [help]: ",
"Possible values are [january], [february], [march], [april], [june] or [all].",
MONTHS + ["all"],
)
# Get user input for day of week (all, monday, tuesday, ... sunday)
day = get_input(
"Please enter day or [help]: ",
"Possible values are [sunday], [monday], [tuesday], [wednesday], [thursday], [friday], [saturday] or [all].",
DAYS + ["all"],
)
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 "all" to apply no month filter
(str) day: Name of the day of week to filter by, or "all" to apply no day filter
Returns:
df: Pandas DataFrame containing city data filtered by month and day
"""
# Load data file into a dataframe
df = pd.read_csv(CITY_DATA[city], index_col=0)
# Convert the Start Time, End Time and Trip Duration columns to datetime and timedelta types.
df["Start Time"] = pd.to_datetime(df["Start Time"])
df["End Time"] = pd.to_datetime(df["End Time"])
df["Trip Duration"] = pd.to_timedelta(df["Trip Duration"], unit="s")
# Extract month and day of week from Start Time to create new columns
df["month"] = df["Start Time"].dt.month_name().str.lower()
df["day_of_week"] = df["Start Time"].dt.day_name().str.lower()
# Filter by month if applicable
if month != "all":
# filter by month to create the new dataframe
df = df[df["month"] == month]
# Filter by day of week if applicable
if day != "all":
# filter by day of week to create the new dataframe
df = df[df["day_of_week"] == day]
return df
def show_raw_data(df):
"""
Function responsible to show raw data 5 rows at a time.
Keeps asking for confirmation to show increments of 5 rows until user enters [no] to stop.
Is executed after loading the dataset and before computing the summary statitics.
Args:
df: Pandas DataFrame containing city data filtered by month and day
Returns:
nothing
"""
print("Would you like to see the raw data?")
while True:
inpt = input("Please enter [y]es or [n]o: ").lower()
if inpt in ["y", "n", "yes", "no"]:
break
else:
print("Sorry, invalid value.")
idx = 0
if inpt in ["y", "yes"]:
while True:
print(df[idx : idx + 5])
while True:
cont = input("Would you like to continue? [y]es or [n]o: ").lower()
if cont in ["y", "n", "yes", "no"]:
break
else:
print("Sorry, invalid value.")
if cont in ["y", "yes"]:
idx += 5
else:
break
def time_stats(df):
"""
Displays statistics on the most frequent times of travel.
Args:
df: Pandas DataFrame containing city data filtered by month and day
Returns:
nothing
"""
print("\nCalculating The Most Frequent Times of Travel...\n")
start_time = time.time()
# Display the most common month
print("The most common month is: {}.".format(df["month"].mode()[0]))
# Display the most common day of week
print("The most common day of week is: {}.".format(df["day_of_week"].mode()[0]))
# Display the most common start hour
df["hour"] = df["Start Time"].dt.hour
print("The most common hour is: {}.".format(df["hour"].mode()[0]))
print("\nThis took %s seconds." % (time.time() - start_time))
print("-" * 40)
def station_stats(df):
"""
Displays statistics on the most popular stations and trip.
Args:
df: Pandas DataFrame containing city data filtered by month and day
Returns:
nothing
"""
print("\nCalculating The Most Popular Stations and Trip...\n")
start_time = time.time()
# Display most commonly used start station
print(
"The most commonly used start station is: {}.".format(
df["Start Station"].mode()[0]
)
)
# Display most commonly used end station
print(
"The most commonly used end station is: {}.".format(df["End Station"].mode()[0])
)
# Display most frequent combination of start station and end station trip
top_comb_start_station, top_comb_end_station = (
df[["Start Station", "End Station"]].value_counts().index[0]
)
print(
"The most frequent combination of start station and end station trip is: {} to {}.".format(
top_comb_start_station, top_comb_end_station
)
)
print("\nThis took %s seconds." % (time.time() - start_time))
print("-" * 40)
def trip_duration_stats(df):
"""
Displays statistics on the total and average trip duration.
Args:
df: Pandas DataFrame containing city data filtered by month and day
Returns:
nothing
"""
print("\nCalculating Trip Duration...\n")
start_time = time.time()
# Display total travel time
print("The total travel time is: {}".format(df["Trip Duration"].sum()))
# display mean travel time
print("The mean travel time is: {}".format(df["Trip Duration"].mean()))
if "User Type" in df.columns:
print("Total travel time per user category:")
user_types_total_travel_time_dict = (
df.groupby(by="User Type")["Trip Duration"].sum().to_dict()
)
for k, v in user_types_total_travel_time_dict.items():
print(" * {}: {}".format(k, v))
print("Mean travel time per user category:")
user_types_mean_travel_time_dict = (
df.groupby(by="User Type")["Trip Duration"].mean().to_dict()
)
for k, v in user_types_mean_travel_time_dict.items():
print(" * {}: {}".format(k, v))
print("Mean travel time per hour of the day:")
hour_mean_travel_time_dict = df.groupby(by="hour")["Trip Duration"].mean().to_dict()
for k, v in hour_mean_travel_time_dict.items():
print(" * {}: {}".format(k, v))
print("\nThis took %s seconds." % (time.time() - start_time))
print("-" * 40)
def user_stats(df):
"""
Displays statistics on bikeshare users.
Args:
df: Pandas DataFrame containing city data filtered by month and day
Returns:
nothing
"""
print("\nCalculating User Stats...\n")
start_time = time.time()
# Display user types count
if "User Type" in df.columns:
user_types_dict = df.groupby(by="User Type")["User Type"].count().to_dict()
print("The users category and quantity are:")
for k, v in user_types_dict.items():
print(" * {}: {}".format(k, v))
# Display gender count
if "Gender" in df.columns:
user_gender_dict = df.groupby(by="Gender")["Gender"].count().to_dict()
print("The number of users by gender is:")
for k, v in user_gender_dict.items():
print(" * {}: {}".format(k, v))
# Display earliest, most recent, and most common year of birth
if "Birth Year" in df.columns:
print(
"The earliest user birth year is: {}.".format(int(df["Birth Year"].min()))
)
print(
"The most recent user birth year is: {}.".format(
int(df["Birth Year"].max())
)
)
print(
"The most common user birth year is: {}.".format(
int(df["Birth Year"].mode())
)
)
print("\nThis took %s seconds." % (time.time() - start_time))
print("-" * 40)
def main():
while True:
city, month, day = get_filters()
df = load_data(city, month, day)
# df = load_data("new york city", "april", "all")
show_raw_data(df)
time_stats(df)
station_stats(df)
trip_duration_stats(df)
user_stats(df)
restart = input("\nWould you like to restart? Enter [yes] or [no]:\n").lower()
if restart.lower() != "yes":
break
if __name__ == "__main__":
main()