forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBikeshare.py
206 lines (158 loc) · 7.44 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
import time
import pandas as pd
import numpy as np
import calendar as cal
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():
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 = input("Please Select one of the city between Chicago, New York City or Washington: ").lower()
if city in ['chicago', 'new york city', 'washington']:
overview = pd.read_csv(CITY_DATA[city])
print('Here the first 5 rows from',city, ':\n', overview.head(5))
while city not in ['chicago', 'new york city', 'washington']:
print('Not Valid')
city = input("Please Select one of the city between Chicago, New York City or Washington again: ").lower()
overview = pd.read_csv(CITY_DATA[city])
print('Here the first 5 rows from',city, ':\n', overview.head(5))
# get user input for month (all, january, february, ... , june)
month = input("Please Select the month between January to June or all: ").lower()
while month not in ['january', 'february', 'march', 'april', 'may', 'june', 'all']:
print('Not Valid')
month = input("Please Select the month between January to June again: ").lower()
# get user input for day of week (all, monday, tuesday, ... sunday)
day = input("Please Select the day of the week or all: ").lower()
while day not in ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'all']:
print('Not Valid')
day = input("Please Select the day of the week again: ").lower()
print('-'*40)
return city, month, day
def load_data(city, month, day):
# load data file into a dataframe
df = pd.read_csv(CITY_DATA[city])
# convert the Start Time column to datetime
df['Start Time'] = pd.to_datetime(df['Start Time'])
# extract month day and hour of week from Start Time to create new columns
df['month'] = df['Start Time'].dt.month
df['day_of_week'] = df['Start Time'].dt.weekday_name
df['hour'] = df['Start Time'].dt.hour
# 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[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.title()]
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
most_common_month = df['month'].mode()[0]
most_common_month_name = cal.month_name[most_common_month]
# display the most common day of week
most_common_day = df['day_of_week'].mode()[0]
# display the most common start hour
most_common_start_hour = df['hour'].mode()[0]
print('Most common month of travel:', most_common_month_name)
print('Most common day of travel:', most_common_day)
print('Most common start hour of travel:', most_common_start_hour)
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
popular_start_station = df['Start Station'].mode()[0]
# display most commonly used end station
popular_end_station = df['End Station'].mode()[0]
# display most frequent combination of start station and end station trip
df['start end station'] = df['Start Station'] + ' and ' + df['End Station']
popular_start_end_station = df['start end station'].mode()[0]
print('Most commonly used start station:', popular_start_station)
print('Most commonly used end station :', popular_end_station)
print('Most frequent combination of start station and end station trip :', popular_start_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."""
print('\nCalculating Trip Duration...\n')
start_time = time.time()
# display total travel time
total_travel_time = df['Trip Duration'].sum()
print('Total travel time : ', total_travel_time, 'seconds.')
# display mean travel time
mean_travel_time = df['Trip Duration'].mean()
print('Mean of travel time : ', mean_travel_time, 'seconds.')
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def user_stats(df):
"""Displays statistics on bikeshare users."""
print('\nCalculating User Stats...\n')
start_time = time.time()
# Display counts of user types
counts_user_types = df['User Type'].value_counts()
print('Counts of user types:\n', counts_user_types, '\n')
# Display counts of gender
if 'Gender' in df.columns:
counts_gender = df['Gender'].value_counts()
print('Counts of gender:\n', counts_gender, '\n')
else:
print('There is no information in Gender')
# Display earliest, most recent, and most common year of birth
if 'Birth Year' in df.columns:
earliest_yob = int(df['Birth Year'].min())
most_recent_yob = int(df['Birth Year'].max())
most_common_yob = int(df['Birth Year'].mode())
print('Earliest year of birth: ', earliest_yob)
print('Most recent year of birth: ', most_recent_yob)
print('Most common year of birth: ', most_common_yob)
else:
print('There is no information in Birth Year')
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def main():
while True:
"""Main body of program"""
city, month, day = get_filters()
df = load_data(city, month, day)
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')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()