-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbikeshare_2.py
271 lines (195 loc) · 8.77 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
import time
import pandas as pd
import numpy as np
import datetime as dt
CITY_DATA = {'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv'}
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
"""
city = None # type: str
month = None # type: str
day = None # type: str
choice = None # type: str or tuple
print('Hello! Let\'s explore some US bikeshare data!')
city_option = ['chicago', 'new york', 'washington']
month_option = ['january', 'february', 'march', 'april', 'may', 'june']
choice_option = ['month', 'day', 'none', 'both']
# get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
while city not in city_option:
city = str(input('Would you like to see data for Chicago, '
'New York City, or Washington?')).lower().strip()
while choice not in choice_option:
choice = str(input("Would you like to filter the data by month, day, both or not at all? "
"Type \"none\" for no time filter. ")).lower().strip()
# for choice is none no filtering is required
if choice == "none":
month = "all"
day = "all"
# for choice is both input month and day
if choice == "both":
choice = 'month','day'
# get user input for month (all, january, february, ... , june)
if 'month' in choice:
while month not in month_option:
month = str(input('Which month? January, February, March, April, May or June. ')).lower().strip()
# get user input for day of week (all, monday, tuesday, ... sunday)
if 'day' in choice:
while day not in ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']:
day = str(input('Which day? Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. ')).lower().strip()
if day == None:
day="all"
if month == None:
month="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
"""
df = pd.read_csv(CITY_DATA[city])
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['End Time'] = pd.to_datetime(df['End Time'])
if month == 'all':
#Add a new column for month as month
df['month']=df['Start Time'].dt.month
else:
# filter by month
month_option = {'january': 1, 'february': 2, 'march': 3, 'april': 4, 'may': 5, 'june': 6}
df = df[df['Start Time'].dt.month == month_option[month]]
if day == 'all':
#Add a new column for day of week as day
df['day']=df['Start Time'].dt.weekday_name
elif day != None:
#filter by day of week
df = df[df['Start Time'].dt.weekday_name == day.title()]
return df
def time_stats(df):
"""Displays statistics on the most frequent times of travel."""
print('\nCalculating the first statistics...\n')
start_time = time.time()
# display the most common month
if 'month' in df.columns:
popular_month = df['month'].mode()[0]
count = df['month'].value_counts(dropna=True)[popular_month]
print("Most popular month:",popular_month," Count:",count)
# display the most common day of week
if 'day' in df.columns:
popular_day = df['day'].mode()[0]
count = df['day'].value_counts(dropna=True)[popular_day]
print("Most popular day of week:",popular_day.title()," Count:",count)
# display the most common start hour
popular_hour = df['Start Time'].dt.hour.mode()[0]
count = df['Start Time'].dt.hour.value_counts(dropna=True)[popular_hour]
print("Most popular hour:",popular_hour," Count:",count)
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 next statistics... Popular Stations and Trip\n')
start_time = time.time()
# display most commonly used start station
popular_start_station = df['Start Station'].mode()[0]
count = df['Start Station'].value_counts(dropna=True)[popular_start_station]
print("Most popular start station:",popular_start_station.title()," Count:",count)
# display most commonly used end station
popular_end_station = df['End Station'].mode()[0]
count = df['End Station'].value_counts(dropna=True)[popular_end_station]
print("Most popular end station:",popular_end_station.title()," Count:",count)
# display most frequent combination of start station and end station trip
df['Trip']=df['Start Station']+' - '+df['End Station']
popular_trip = df['Trip'].mode()[0]
count = df['Trip'].value_counts(dropna=True)[popular_trip]
print("Most popular trip:",popular_trip.title()," Count:",count)
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 the next statistics... Trip Duration...\n')
start_time = time.time()
# display total travel time
total_travel_time = df['Trip Duration'].sum(skipna=True)
count=df['Trip Duration'].count()
# display mean travel time
average_travel_time = total_travel_time/(count)
print("Total Duration:",total_travel_time," Count:",count,
" Average Duration:",average_travel_time)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-' * 40)
def user_stats(df):
"""Displays statistics on bikeshare users."""
print('\nCalculating the next statistics... User Stats...\n')
start_time = time.time()
# Display counts of user types
count=df['User Type'].value_counts(dropna=True)
subscriber=count['Subscriber']
customer=count['Customer']
print("Subscriber:",subscriber," Customer:",customer)
# Display counts of gender
if 'Gender' in df.columns:
gender=df['Gender'].value_counts(dropna=True)
male=gender['Male']
female=gender['Female']
print("Male:",male," Female:",female)
# Display earliest, most recent, and most common year of birth
if 'Birth Year' in df.columns:
#Display earliest birth year
earliest_year=df['Birth Year'].min(skipna=True)
count = df['Birth Year'].value_counts(dropna=True)[earliest_year]
print("Earliest year:",earliest_year," Count:",count)
#Display most recent year
recent_year=df['Birth Year'].max(skipna=True)
count = df['Birth Year'].value_counts(dropna=True)[recent_year]
print("Recent year:",recent_year," Count:",count)
#Display most popular Year
popular_year=df['Birth Year'].mode()[0]
count = df['Birth Year'].value_counts(dropna=True)[popular_year]
print("Popular year:",popular_year," Count:",count)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-' * 40)
def raw_output_data(df):
raw_output = str(input('\nWould you like to view individual trip data? '
'Enter yes or no.\n')).strip().lower()
count = 0
if raw_output.lower() == 'yes':
print('\nAccessing Raw Data...\n')
start_time = time.time()
count = 0 #row count
#loop through csv data
while True:
pd.set_option('display.max_columns', 30)
print(df.iloc[count:count + 5])
count += 5
print("\nThis took %s seconds." % (time.time() - start_time))
raw_output = input('\nWould you like to see 5 more rows of raw data? (Yes or No)\n> ').lower()
# breaks out of loop if user doesn't type "yes"
if raw_output != 'yes':
break
print('-'*40)
return df
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)
raw_output_data(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()