-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbikeshare.py
221 lines (170 loc) · 7.81 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import time
import pandas as pd
import numpy as np
CITY_DATA = { 'chicago': 'chicago.csv',
'new york': '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
"""
invalid_inputs = "Invalid input. Please try again"
print('Hello! Let\'s explore some US bikeshare data!')
# TO DO: get user raw_input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
while 1 == 1 :
city = raw_input("\nenter the name of the city to analyze city names are as follows\nchicago,\nnew york,\nwashington. \n").lower()
if city in ['chicago', 'new york', 'washington']:
break
else:
print(invalid_inputs)
# TO DO: get user raw_input for month (all, january, february, ... , june)
while 1 == 1 :
month = raw_input("\nenter the name of the month\njanuary,\nfebruary,\nmarch,"
"\napril,\nmay,\njune\nto filter by, or \"all\" to apply no month filter\n").lower()
if month in ["january", "february", "march", "april", "may", "june", "all"]:
break
else:
print(invalid_inputs)
# TO DO: get user raw_input for day of week (all, monday, tuesday, ... sunday)
while 1 == 1 :
day = raw_input("\nenter the name of the day\nmonday,\ntuesday,\nwednesday,\nthursday,"
"\nfriday,\nsaturday,\nsunday\nof week to filter by, or \"all\" to apply no day filter\n").lower()
if day in ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday", "all"]:
break
else:
print(invalid_inputs)
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
"""
file_name = CITY_DATA[city]
print ("Accessing data from: " + file_name)
df = pd.read_csv(file_name)
# convert the Start Time column to datetime
df['Start Time'] = pd.to_datetime(arg = df['Start Time'], format = '%Y-%m-%d %H:%M:%S')
# filter by month if applicable
if month != 'all':
# extract month and day of week from Start Time to create new columns
df['month'] = df['Start Time'].dt.month
# 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':
df['day_of_week'] = df['Start Time'].dt.weekday_name
# filter by day of week to create the new dataframe
df = df.loc[df['day_of_week'] == day.title()]
return df
def time_stats(df):
"""Displays statistics on the most frequent times of travel."""
print('\nMost Frequent Times of Travel...\n')
start_time = time.time()
# Convert the Start Time column to datetime
df['Start Time'] = pd.to_datetime(arg = df['Start Time'], format = '%Y-%m-%d %H:%M:%S')
# Create new columns for month, weekday, hour
month = df['Start Time'].dt.month
weekday_name = df['Start Time'].dt.weekday_name
hour = df['Start Time'].dt.hour
# TO DO: display the most common month
most_common_month = month.mode()[0]
print('Most common month: ', most_common_month)
# TO DO: display the most common day of week
most_common_day_of_week = weekday_name.mode()[0]
print('Most common day of week: ', most_common_day_of_week)
# TO DO: display the most common start hour
common_start_hour = hour.mode()[0]
print('Most frequent start hour: ', 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('\nMost Popular Stations and Trip...\n')
start_time = time.time()
# TO DO: display most commonly used start station
print('Most commonly used start station:', df['Start Station'].value_counts().idxmax())
# TO DO: display most commonly used end station
print('Most commonly used end station:', df['End Station'].value_counts().idxmax())
# TO DO: display most frequent combination of start station and end station trip
combine_stations = df['Start Station'] + "*" + df['End Station']
common_station = combine_stations.value_counts().idxmax()
print('Most frequent used combinations are:\n{} \nto\n{}'.format(common_station.split('*')[0], common_station.split('*')[1]))
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()
# Convert seconds to readable time format
def secs_to_readable_time(seconds):
m, s = divmod(seconds,60)
h, m = divmod(m,60)
d, h = divmod(h,24)
y, d = divmod(d,365)
print('Years: {}, Days: {}, Hours: {}, Mins: {}, Secs: {}'.format(y,d,h,m,s))
# TO DO: display total travel time
total_travel_time = df['Trip Duration'].sum()
print('Total travel time:\n')
secs_to_readable_time(total_travel_time)
# TO DO: display mean travel time
mean_travel_time = df['Trip Duration'].mean()
print('\nMean travel time: {} seconds'.format(mean_travel_time))
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()
# TO DO: Display counts of user types
user_types = df['User Type'].value_counts()
print(user_types)
# TO DO: Display counts of gender
if 'Gender' in df.columns:
gender_count = df['Gender'].value_counts()
print(gender_count)
# TO DO: Display earliest, most recent, and most common year of birth
if 'Birth Year' in df.columns:
earliest_birth_year = df['Birth Year'].min()
most_recent_birth_year = df['Birth Year'].max()
common_birth_year = df['Birth Year'].mode()[0]
print("\nEarliest year of birth: " + str(earliest_birth_year))
print("\nMost recent year of birth: " + str(most_recent_birth_year))
print("\nMost common year of birth: " + str(common_birth_year))
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def raw_data(df):
user_input = raw_input('Do you want to see raw data? Enter yes or no.\n')
line_number = 0
while 1 == 1 :
if user_input.lower() != 'no':
print(df.iloc[line_number : line_number + 5])
line_number += 5
user_input = raw_input('\nDo you want to see more raw data? Enter yes or no.\n')
else:
break
def main():
while 1 == 1 :
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_data(df)
restart = raw_input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()