-
Notifications
You must be signed in to change notification settings - Fork 0
/
bikeshare.py
195 lines (149 loc) · 6.78 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
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' }
valid_city = ['chicago', 'new york city', 'washington']
valid_month = ['all', 'january', 'febuary', 'march', 'april', 'may', 'june', 'july', 'september', 'october', 'november', 'december']
valid_day= ['all', '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!')
# TO DO: get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
while True:
city = str(input("Choose name of city (chicago, new york city, washington): ")).lower()
if city in valid_city:
break
else:
print("Invalid city name - try again")
# TO DO: get user input for month (all, january, february, ... , june)
while True:
month = str(input("Choose name of month (all, january, february, ... , december): ")).lower()
if month in valid_month:
break
else:
print("Invalid month name - try again")
# TO DO: get user input for day of week (all, monday, tuesday, ... sunday)
while True:
day = str(input("Choose name of day (all, monday, tuesday, ... sunday): ")).lower()
if day in valid_day:
break
else:
print("Invalid day name - try again")
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['month'] = df['Start Time'].dt.month
df['day'] = df['Start Time'].dt.day_name()
if month != 'all':
month = valid_month.index(month)
df = df[df['month'] == month]
if day != 'all':
df = df[df['day'] == 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()
try:
# TO DO: display the most common month
print("the most common month is {}".format(df['month'].mode().max()))
# TO DO: display the most common day of week
print("the most common day of week is {}".format(df['day'].mode().max()))
# TO DO: display the most common start hour
df['hour'] = df['Start Time'].dt.hour
print("the most common start hour is {}".format(df['hour'].mode().max()))
except:
print("*************An error occurred*************")
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()
try:
# TO DO: display most commonly used start station
print("The most commonly used start station is {}".format(df['Start Station'].mode().max()))
# TO DO: display most commonly used end station
print("The most commonly used end station is {}".format(df['End Station'].mode().max()))
# TO DO: display most frequent combination of start station and end station trip
print("The most frequent combination of start station and end station trip are\n{}".format(df[['Start Station', 'End Station']].mode().max()))
except:
print("*************An error occurred*************")
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()
try:
# TO DO: display total travel time
print("total travel time is {}".format(df["Trip Duration"].sum()))
# TO DO: display mean travel time
print("mean travel time is {}".format(df["Trip Duration"].sum()/len(df["Trip Duration"])))
except:
print("*************An error occurred*************")
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()
try:
# TO DO: Display counts of user types
print("The counts of user types are\n{}".format(df['User Type'].value_counts()))
# TO DO: Display counts of gender
print("The counts of genders are\n{}".format(df['Gender'].value_counts()))
# TO DO: Display earliest, most recent, and most common year of birth
print("The earliest year of birth is {}".format(df['Birth Year'].min()))
print("The most recent year of birth is {}".format(df['Birth Year'].max()))
print("The most common year of birth is {}".format(df['Birth Year'].mode().max()))
except:
print("*************An error occurred*************")
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def display_data(df):
"""Displays rows of data from the dataframe to the user"""
display = input('\nWould you like to view the first 5 rows of data? Enter yes or no.\n').lower()
loc = 0
while(display == 'yes'):
start_time = time.time()
for _ in range(5):
print(df.iloc[loc])
loc += 1
print("\nThis took %s seconds." % (time.time() - start_time))
display = input("Do you wish to view the next 5 rows of data? Enter yes or no.\n").lower()
print('-'*40)
def main():
while True:
city, month, day = get_filters()
df = load_data(city, month, day)
#Trip statistics
time_stats(df)
station_stats(df)
trip_duration_stats(df)
user_stats(df)
display_data(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()