forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbikeshare.py
More file actions
213 lines (168 loc) · 7.3 KB
/
Copy pathbikeshare.py
File metadata and controls
213 lines (168 loc) · 7.3 KB
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
import time
import pandas as pd
import numpy as np
CITY_DATA = { 'chicago': 'chicago.csv',
'new york': 'new_york_city.csv',
'washington': 'washington.csv' }
cities = ["chicago", "new york", "washington"]
months = ["january", "february", "march", "april", "may", "june", "all"]
days_of_week = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday", "all"]
df = None
def get_filters():
"""
Asks user to specify a city, month, and day to analyze.
Returns:
(string) city - name of the city to analyze
(string) month - name of the month to filter by, or "all" to apply no month filter
(string) 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 = input("Which data would you like to analyze: chicago, new york or washington? ").lower()
if city in cities:
print("alright")
else:
print('Looks like you made a spelling mistake - please try again')
break
else:
print("Please select from chicago, new york, or washington")
# TO DO: get user input for month (all, january, february, ... , june)
while True:
month = input("Which month would you like to analyze- january, february, march, april, may, june or all? ").lower()
if month in months:
print("alright")
break
else:
print("Sorry!Please select from all or one month from january, february, march, april, may, june")
# TO DO: get user input for day of week (all, monday, tuesday, ... sunday)
while True:
day = input("Which day would you like to analyze - monday, tuesday, wednesday, thursday, friday, saturday, sunday or all? ").lower()
if day in days_of_week:
print("alright")
break
else:
print("Sorry!Please name one weekday or 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
"""
city_file = CITY_DATA[city]
df = pd.read_csv(city_file)
df = pd.DataFrame(df)
print(city, city_file)
print('-'*40)
return df
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()
# TO DO: display the most common month
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['month'] = df['Start Time'].dt.month
popular_month = df['month'].mode()[0]
print('Most Popular Start Month:', popular_month)
# TO DO: display the most common day of week
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['weekday'] = df['Start Time'].dt.weekday
popular_weekday = df['weekday'].mode()[0]
print('Most Popular Start weekday:', popular_weekday)
# TO DO: display the most common start hour
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['hour'] = df['Start Time'].dt.hour
popular_hour = df['hour'].mode()[0]
print('Most Popular Start hour:', popular_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()
# TO DO: display most commonly used start station
popular_start_station = df['Start Station'].mode()[0]
print('Most Popular Start station:', popular_start_station)
# TO DO: display most commonly used end station
popular_end_station = df['End Station'].mode()[0]
print('Most Popular End station:', popular_end_station)
# TO DO: display most frequent combination of start station and end station trip
Combination = df['Start Station'] + ' to ' + df['End Station']
print('The most common start and end station is', Combination.mode()[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()
# TO DO: display total travel time
mean_travel = df['Trip Duration'].sum()
print("The total of trip duration is", mean_travel)
# TO DO: display mean travel time
total_travel = df['Trip Duration'].mean()
print("The mean of trip duration is", total_travel)
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
if 'User Type' in df.columns:
counts_user_types = df['User Type'].value_counts()
print("The counts_user_types is")
for i in range(len(counts_user_types.index.values)):
print(counts_user_types.index.values[i], ' : ', list(counts_user_types)[i])
else:
print("More user Type Data is not available")
# TO DO: Display counts of gender
counts_gender = df['Gender'].value_counts()
print("The counts_gender is:")
for i in range(len(counts_gender.index.values)):
print(counts_gender.index.values[i], ' : ', list(counts_gender)[i])
else:
print("More Gender Data not available")
# TO DO: Display earliest, most recent, and most common year of birth
# earliest year of birth
if 'Birth Year' in df.columns:
earliest_year = df['Birth Year'].min()
print("The earliest_year of birth is", int(earliest_year))
# most recent year of birth
most_recent_year = df['Birth Year'].max()
print("The most_year of birth is", int(most_recent_year))
# most common year of birth
most_common_year = df['Birth Year'].mode()
print("The most_common_year of birth is", int(most_common_year))
else:
print("Birth Year Data not available")
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def display_data(df):
"Asks user to display the first 5 data rows"
view_data = input('\nWould you like to view 5 rows of individual trip data? Enter yes or no\n')
start_loc = 0
while view_data == 'yes':
print(df.iloc[start_loc:start_loc+5])
start_loc += 5
view_data = input('Do you wish to continue?: ').lower()
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)
display_data(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()