forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbikeshare_2.py
279 lines (219 loc) · 10 KB
/
bikeshare_2.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
# Jong Min (Jay) Lee_Udacity Programming for Data Science_Project 2: Python
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']
dow = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']
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).
print('\nEnter Inputs for Exploring US Bikeshare Data')
while True:
print('\n1. Would you like to see data for Chicago, New York City, or Washington?')
city = input(' See data for the city: ').lower()
if city in CITY_DATA:
break
else:
print('\n [Invalid Entry] Please enter one of the three cities.')
# get user input for month and day of week
while True:
print('\n2. Would you like to filter the data by month, day, both or not at all (enter "None")?')
filter_by = input(' Filter the data by: ').lower()
if filter_by == 'none':
month = 'all'
day = 'all'
break
elif filter_by == 'month':
day = 'all'
month = get_filters_month()
break
elif filter_by == 'day':
month = 'all'
day = get_filters_day()
break
elif filter_by == 'both':
day = get_filters_day()
month = get_filters_month()
break
else:
print('\n [Invalid Entry] Please enter month, day, both or None.')
print('\n [Request] Explore Bikeshare Data for {} with filters month: {}, day: {}\n'.format(city.title(), month.title(), day.title()))
print('-'*40)
return city, month, day
def get_filters_month():
"""
If the user requests to filter data by month,
then run get_filters_month() and return the specific month by which the data needs to be filtered.
"""
while True:
filter_by_month = input('\n Filter the data by the month: ').lower()
if filter_by_month in months:
break
else:
print('\n [Invalid Entry] Please enter a month from January to June.')
return filter_by_month
def get_filters_day():
"""
If the user requests to filter data by day,
then run get_filters_day() and return the specific day by which the data needs to be filtered.
"""
while True:
filter_by_day = input('\n Filter the data by the day of the week: ').lower()
if filter_by_day in dow:
break
else:
print('\n [Invalid Entry] Please enter a day from Monday to Sunday.')
return filter_by_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
"""
print('\nLoading .csv data ...')
df = pd.read_csv(CITY_DATA[city])
# Apply datetime properties to Start Time column values
df['Start Time'] = pd.to_datetime(df['Start Time'])
# Create new columns for month and day of week
df['month'] = df['Start Time'].dt.month
df['day_of_week'] = df['Start Time'].dt.weekday_name
# both or by month only
if month != 'all':
month_num = months.index(month) + 1
df = df[df['month'] == month_num]
print('[Loading] applied filter for month: {}...'.format(month.title()))
# both or by day only
if day != 'all':
df = df[df['day_of_week'] == day.title()]
print('[Loading] applied filter for day: {}...'.format(day.title()))
print('\n Completed loading .csv data for {} with filters month: {}, day: {}\n'.format(city.title(), month.title(), day.title()))
print('-'*40)
return df
def time_stats(df):
"""Displays statistics on the most frequent times of travel."""
print('\n[Analysis 1/4] Calculating The Most Frequent Times of Travel...\n')
start_time = time.time()
# display the most common month (if filter applied for month)
if len(df['month'].unique()) != 1:
popular_month_num = df['month'].mode()[0]
print(' - Most popular month: {}'.format(months[popular_month_num - 1].title()))
# display the most common day of week (if filter applied for day)
if len(df['day_of_week'].unique()) != 1:
print(' - Most popular day: {}'.format(df['day_of_week'].mode()[0]))
# display the most common start hour
df['hour'] = df['Start Time'].dt.hour
print(' - Most popular hour: {}'.format(df['hour'].mode()[0]))
print("\n Completed calculating most frequent times of travel (%s seconds).\n" % (format(time.time() - start_time,'.4f')))
print('-'*40)
def station_stats(df):
"""Displays statistics on the most popular stations and trip."""
print('\n[Analysis 2/4] Calculating The Most Popular Stations and Trip...\n')
start_time = time.time()
print(' - Most commonly used Stations')
popular_start_st = df['Start Station'].mode()[0]
popular_start_st_count = df['Start Station'].value_counts()[0]
print(' Start: {} ({} times)'.format(popular_start_st,popular_start_st_count))
popular_end_st = df['End Station'].mode()[0]
popular_end_st_count = df['End Station'].value_counts()[0]
print(' End: {} ({} times)'.format(popular_end_st,popular_end_st_count))
# display most frequent combination of start station and end station trip
df['Start End St'] = df['Start Station'] +','+ df['End Station']
print(' - Most frequent combination of Stations: ({} times)'.format(df['Start End St'].value_counts()[0]))
for i, station in enumerate(df['Start End St'].mode()[0].split(',')):
if i == 0:
print(' Start: {}'.format(station))
else:
print(' End: {}'.format(station))
print("\n Completed calculating most popular stations and trip (%s seconds).\n" % (format(time.time() - start_time,'.4f')))
print('-'*40)
def trip_duration_stats(df):
"""Displays statistics on the total and average trip duration."""
print('\n[Analysis 3/4] Calculating Trip Duration...\n')
start_time = time.time()
# display total travel time
print(' - Total Travel Time: {} seconds'.format(df['Trip Duration'].sum()))
# display mean travel time
print(' - Mean Travel Time : {} seconds'.format(df['Trip Duration'].mean()))
print("\n Completed calculating trip duration (%s seconds).\n" % (format(time.time() - start_time,'.4f')))
print('-'*40)
def user_stats(df, city):
"""Displays statistics on bikeshare users.
Arg city: filters Washington with no bikeshare data for Gender and Birth Year
"""
print('\n[Analysis 4/4] Calculating User Stats...\n')
start_time = time.time()
# Display counts of user types
print(' - User Type:')
for usertype in df['User Type'].unique():
print(' {}: {}'.format(usertype,df[df['User Type']==usertype]['User Type'].count()))
if city in ['chicago','new york city']:
# Display counts of gender
print(' - Gender:')
for gender in df['Gender'].unique():
print(' {}: {}'.format(gender,df[df['Gender']==gender]['Gender'].count()))
# Display earliest, most recent, and most common year of birth
print(' - Birth Year:')
print(' Earliest : {}'.format(int(df['Birth Year'].min())))
print(' Most Recent : {}'.format(int(df['Birth Year'].max())))
common_yob = df['Birth Year'].mode()[0]
common_yob_count = df[df['Birth Year']==common_yob]['Birth Year'].count()
print(' Most Common Year: {} ({} times)'.format(int(common_yob),common_yob_count))
else:
print(' - Gender and Birth Year data not available for {}'.format(city))
print("\n Completed calculating user stats (%s seconds).\n" % (format(time.time() - start_time,'.4f')))
print('-'*40)
def top_n(df):
"""See top # records of the data frame."""
while True:
top_n = input('\nWould you like to see top # records? [Yes or No] ').lower()
if top_n == 'yes':
while True:
try:
n = int(input('\nHow many records would you like to see?\nEnter a number from 1 to 10: '))
except:
print('\n [Invalid Entry] Please try again.')
else:
if n in range(1,11):
i = 1
for row in df.head(n).index:
print('\n{}th Record'.format(i))
for column in df.columns[1:-3]:
print(' - {}: {}'.format(column,df[column][row]))
i += 1
break
else:
print('\n [Invalid Entry] Please try again.')
break
elif top_n == 'no':
print('\n Top # records will not be displayed.')
break
else:
print('\n [Invalid Entry] Please enter Yes or No.')
def main():
while True:
city, month, day = get_filters()
df = load_data(city, month, day)
time_stats(df)
station_stats(df)
trip_duration_stats(df)
user_stats(df,city)
top_n(df)
restart = input('\nEnter yes to perform another analysis. Otherwise, enter any characters.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()