forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bikeshare.py
227 lines (174 loc) · 6.92 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
222
223
224
225
226
227
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' }
def get_filters(city, month, day):
"""
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!')
# get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
while True:
city = input("Write a city name: Chicago, New York City or Washington!\n").lower()
if city not in CITY_DATA:
print("\nInvalid answer\n")
continue
else:
break
while True:
time = input("Do you want to filter as month, day, all or none?\n").lower()
if time == 'month':
print(("Which month? January, Feburary, March, April, May or June?\n"))
month = input("Please note that months beyond june is out of the scope of my project \n").lower()
day = 'all'
break
elif time == 'day':
month = 'all'
day = input("Which day? Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday\n").lower()
break
elif time == 'all':
print(("Which month? January, Feburary, March, April, May or June?\n"))
month = input("Please note that months beyond june is out of the scope of my project \n").lower()
day = input("Which day? Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday\n").lower()
break
elif time == 'none':
month = 'all'
day = 'all'
break
else:
input("You wrote the wrong word! Please type it again. month, day, all or none?\n")
break
print(city)
print(month)
print(day)
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_of_week'] = df['Start Time'].dt.day_name()
if month != 'all':
months = ['january', 'february', 'march', 'april', 'may', 'june']
month = months.index(month) +1
df = df[df['month'] == month]
if day != 'all':
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
common_month = df['month'].mode()[0]
print(common_month)
# display the most common day of week
common_day_of_week = df['day_of_week'].mode()[0]
print(common_day_of_week)
# display the most common start hour
df['hour'] = df['Start Time'].dt.hour
common_hour = df['hour'].mode()[0]
print(common_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
common_start = df['Start Station'].mode()[0]
print(common_start)
# display most commonly used end station
common_end = df['End Station'].mode()[0]
print(common_end)
# display most frequent combination of start station and end station trip
df['combination'] = df['Start Station'] + ' to ' + df['End Station']
common_combination = df['combination'].mode()[0]
print(common_combination)
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 = df['Trip Duration'].sum()
print(total_travel)
# display average (mean) travel time
mean_travel = df['Trip Duration'].mean()
print(mean_travel)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def user_stats(df):
"""Displays statistics on bikeshare users."""
print('\n\nCalculating User Stats...\n')
start_time = time.time()
# Display counts of user types
user_types = df['User Type'].value_counts()
print(user_types)
# Display counts of gender
if 'Gender' in df:
gender = df['Gender'].value_counts()
print(gender)
else:
print("There is no gender information in this city.")
# Display earliest, most recent, and most common year of birth
if 'Birth_Year' in df:
earliest = df['Birth_Year'].min()
print(earliest)
recent = df['Birth_Year'].max()
print(recent)
common_birth = df['Birth Year'].mode()[0]
print(common_birth)
else:
print("There is no birth year information in this city.")
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
"""Asking 5 lines of the raw data and more, if they want"""
def data(df):
raw_data = 0
while True:
answer = input("Do you want to see the raw data? Yes or No").lower()
if answer not in ['yes', 'no']:
answer = input("You wrote the wrong word. Please type Yes or No.").lower()
elif answer == 'yes':
raw_data += 5
print(df.iloc[raw_data : raw_data + 5])
again = input("Do you want to see more? Yes or No").lower()
if again == 'no':
break
elif answer == 'no':
return
def main():
city = ""
month = ""
day = ""
while True:
city, month, day = get_filters(city, month, day)
df = load_data(city, month, day)
time_stats(df)
station_stats(df)
trip_duration_stats(df)
user_stats(df)
data(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
print("Thank you for Exploring \n Hope you Enjoyed!!!")
break
if __name__ == "__main__":
main()