forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bikeshares.py
270 lines (212 loc) · 8.64 KB
/
bikeshares.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
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'}
def concat(l1, l2):
List = []
for word in l1:
List.append(word)
for word in l2:
List.append(word)
return List
def most_frequent(List):
word_counter = {}
for word in List:
if word not in word_counter:
word_counter[word] = 1
else:
word_counter[word] += 1
return word_counter[max(word_counter)]
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! \n')
while True:
try:
city = input('Would you like to see data for Chicago, New York City, or Washington ? \n')
if city.lower() in ('chicago', 'new york city', 'washington'):
city = city.lower()
break
else:
print('Please choose between these cities : Chicago, New York City, Washington \n')
except ValueError:
print('wrong value \n')
while True:
try:
time = input(
'Would you like to filter data by month, day, both, or not at all ? Type "none" for no time filter \n')
if time.lower() == 'both':
while True:
month = input('Which month? January, February, March, April, May, June \n')
if month.lower() in ('january', 'february', 'march', 'april', 'may', 'june'):
month = month.lower()
while True:
day = input(
'Which day? Please between these days : all,'
' Monday, Tuesday, Wednesday, Thursday, Saturday, Sunday \n')
if day.lower() in (
'all', 'monday', 'tuesday', 'wednesday', 'thursday', 'saturday', 'sunday'):
day = day.lower()
break
else:
print('Please choose day like this : 0= all, 1=Sunday,2=monday,3=tuesday,... \n')
break
else:
print('Please choose month between January, February, March, April, May, June \n')
break
elif time.lower() == 'month':
while True:
month = input('Which month? January, February, March, April, May, June \n')
if month.lower() in ('january', 'february', 'march', 'april', 'may', 'june'):
month = month.lower()
day = 'all'
break
else:
print('Please choose month between January, February, March, April, May, June \n')
break
except:
print('error')
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
"""
df = pd.read_csv("{}.csv".format(city.replace(" ", "_")))
# Convert the Start and End Time columns to datetime
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['End Time'] = pd.to_datetime(df['End Time'])
# extract month and day of week from Start Time to create new columns
df['month'] = df['Start Time'].apply(lambda x: x.month)
df['day_of_week'] = df['Start Time'].apply(lambda x: x.strftime('%A').lower())
# filter by month if applicable
if month != 'all':
# use the index of the months list to get the corresponding int
months = ['january', 'february', 'march', 'april', 'may', 'june']
month = months.index(month) + 1
# filter by month to create the new dataframe
df = df.loc[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.loc[df['day_of_week'] == day, :]
return df
def time_stats(df):
"""Displays statistics on the most frequent times of travel."""
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(
str(df['month'].mode().values[0]))
)
# display the most common day of week
print("The most common day of the week: {}".format(
str(df['day_of_week'].mode().values[0]))
)
# display the most common start hour
df['start_hour'] = df['Start Time'].dt.hour
print("The most common start hour: {}".format(
str(df['start_hour'].mode().values[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."""
print('\nCalculating The Most Popular Stations and Trip...\n')
start_time = time.time()
# display most commonly used start station
print("The most common start station is: {} ".format(
df['Start Station'].mode().values[0])
)
# display most commonly used end station
print("The most common end station is: {}".format(
df['End Station'].mode().values[0])
)
# display most frequent combination of start station and end station trip
df['routes'] = df['Start Station'] + " " + df['End Station']
print("The most common start and end station combo is: {}".format(
df['routes'].mode().values[0])
)
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."""
print('\nCalculating Trip Duration...\n')
start_time = time.time()
df['duration'] = df['End Time'] - df['Start Time']
# display total travel time
print("The total travel time is: {}".format(
str(df['duration'].sum()))
)
# display mean travel time
print("The mean travel time is: {}".format(
str(df['duration'].mean()))
)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-' * 40)
def user_stats(df, city):
"""Displays statistics on bikeshare users."""
print('\nCalculating User Stats...\n')
start_time = time.time()
# Display counts of user types
print("Here are the counts of various user types:")
print(df['User Type'].value_counts())
if city != 'washington':
# Display counts of gender
print("Here are the counts of gender:")
print(df['Gender'].value_counts())
# Display earliest, most recent, and most common year of birth
print("The earliest birth year is: {}".format(
str(int(df['Birth Year'].min())))
)
print("The latest birth year is: {}".format(
str(int(df['Birth Year'].max())))
)
print("The most common birth year is: {}".format(
str(int(df['Birth Year'].mode().values[0])))
)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-' * 40)
def display_data(df):
"""
Display contents of the CSV file to the display as requested by
the user.
"""
start_loc = 0
end_loc = 5
display_active = input("Do you want to see the raw data?: ").lower()
if display_active == 'yes':
while end_loc <= df.shape[0] - 1:
print(df.iloc[start_loc:end_loc, :])
start_loc += 5
end_loc += 5
end_display = input("Do you wish to continue?: ").lower()
if end_display == 'no':
break
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)
display_data(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()