forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExplore_BikeShare_Interactive.py
339 lines (276 loc) · 13.7 KB
/
Explore_BikeShare_Interactive.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#####
##### Sondos Aabed Explores the Bikeshare Dataset
#####
### Importing the necessary libraries
import time
import pandas as pd
import traceback # I used this to trace back the error catched
#### this is the csv files dictionary
CITY_DATA = { 'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv' }
## in this method i take the user input and handle the entries to make sure they are valid
def entery_validation(input_message, valid_inputs, invalid_messgae):
"""
Function that verifies the user input and if there was a problem it returns a prompt
Args:
(str) input_message - the message displayed to ask the user of input
(list) valid_inputs - a list of enteries that are valid
(str) invalid_messgae - a message to be displayed if the input is invalid
Returns:
(str) input - returns the input when it's valid
"""
## while
while True:
input_value = str(input("\n"+ input_message +"\n"))
input_value = input_value.lower()
if input_value not in valid_inputs:
print(invalid_messgae)
continue
else:
break
return input_value
#### in this method get the filters inputted by the user
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('\nHello! Let\'s explore some US bikeshare data!')
#####
# In those cases an invalid input is handled by asking the user to try again until it's true input
# because this is a redundunt code It was suggested to create anpther method that takes a message and returns the input if valid
####
""" City input """
city_input_message = "Which City would like to explore? All, Chicago, New york city, Or Washington?"
city_invalid_message = "Try to enter another city that is either: Chicago, New york city, Or Washington "
city_valid_enteries = ('all','new york city', 'chicago', 'washington')
# get user input for city (chicago, new york city, washington).
city = entery_validation(city_input_message, city_valid_enteries,city_invalid_message)
""" Month input """
month_input_message = "In which of the months you want to explore? is it (all, january, february, ... , june)"
month_invalid_message = "Try to enter the month again, it wasn't a valid month!"
month_valid_enteries = ('all','january','february','march','april','may','june','july','august','september','october','november','december')
# get user input for month (all, january, february, ... , june)
month = entery_validation(month_input_message, month_valid_enteries, month_invalid_message)
""" Day input """
day_input_messgae = "What about the day you are looking for? is it (all, monday, tuesday, ... sunday)?"
day_inavlid_message = "You entered a not valid day, try again"
day_valid_enteries = ('sunday','monday','all','tuesday','wednesday','thursday','friday','saturday')
# get user input for day of week (all, monday, tuesday, ... sunday)
day = entery_validation(day_input_messgae, day_valid_enteries, day_inavlid_message)
print('-'*40)
return city, month, day
# in this method load the dataset based on which city the user inputs
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
"""
# read the csv file using read_csv pandas based on the user input of cit
# I have decided to add the option all because why not exploring all of them together giving a broader view
if city != 'all':
df = pd.read_csv(CITY_DATA[city])
else:
# for all dataframes if the user choses all concate them
dfs = []
for city, path in CITY_DATA.items(all):
dfC = pd.read_csv(path)
dfs.append(dfC)
df = pd.concat(dfs, ignore_index=True)
## print(df)
return df
## this metohd I created to clean the data
## cleaning the data included handling missing data
## I have also handled the high cardinality of dates
def clean_data(df, city):
"""
Args:
(pandas dataframe) df - takes a data frame with missing data probabloy and with not proper datatypes probably
(city) df - because in the case of washington some coulmns doesn't exists
Returns:
(pandas dataframe) df - imputed with unknown and date handled
"""
df = handle_dates(df, city)
df = handle_missing(df)
return df
# this method I created to handle the missing data
def handle_missing(df):
# when I have created the method display data I have notived that there
# is a missing coulmn name so I searched for it stands for on kaggle
# and it makes since that this is the bike ID, I think in this case
# the bike ID is irrelvant so I made the decision to drop it
# althought a possible query comes to mind what if there is a frequent bike ID for example
# in this project scope it is decided to drop it then
# print(df.columns) it is at index 0
df.drop(df.columns[0], axis = 1, inplace=True)
# I chose to fill them with Unknown
print('We have {} missing enteries'.format(df.isnull().sum().sum()) )
# fill Nan values using fillna method
df.fillna('Unknown', inplace=True)
print('These were filled by (Unknown) ')
return df
## this method I created to handle the dates
def handle_dates(df, city):
"""
Handle the dates as their datatypes using to_datetime pandas
"""
# convert to the proper data type
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['End Time'] = pd.to_datetime(df['End Time'])
## this coulmn has high cardinality so I better create new coulmns that I can filter by
# Like the day of the week and the month and the year and the time
df['start_month'] = df['Start Time'].dt.strftime('%B').str.lower()
df['start_day'] = df['Start Time'].dt.strftime('%A').str.lower()
df['start_year'] = df['Start Time'].dt.strftime('%Y')
df['start_time'] = df['Start Time'].dt.strftime('%X')
df['end_month'] = df['End Time'].dt.strftime('%B').str.lower()
df['end_day'] = df['End Time'].dt.strftime('%A').str.lower()
df['end_year'] = df['End Time'].dt.strftime('%Y')
df['end_time'] = df['End Time'].dt.strftime('%X')
if city in ('new york city', 'chicago'):
df['Birth Year'] = pd.to_datetime(df['Birth Year'])
# we have also the coulmn of Birth year
# df['Birth Year'] = pd.to_datetime(df['Birth Year'], format='%Y')
# this is not working for users stats
# I have decided to handle this one as integer to get the min and max values
df['Birth Year'] = pd.to_numeric(df['Birth Year'],errors='coerce' , downcast='integer')
# dropped them after I handeld them
df.drop('Start Time', axis=1, inplace=True)
df.drop('End Time', axis=1, inplace=True)
return df
# In this function I ask the user if they want to see 5 of the rows
# I use the head method build in by pandas to do that
def display_data(df):
view_data = input('\nWould you like to view 5 rows of individual trip data? Enter yes or no\n').lower()
start_locaction = 0
# I actually will famalrize myself with df.iloc, I like the suggestion, the idea that I went for here that also came to my mind is
# using the head function with its parameter
while view_data == 'yes':
# while the usr wish to print print
# print(df.head(start_locaction))
# So I started this solution but It doesn't actually perform this functionality
# it prints from the first
# So I will go for the suggested way hhhhhh
#using iloc
print(df.iloc[start_locaction:start_locaction+5])
# change the start location of the head print
start_locaction=start_locaction +5
view_data = input("Do you want to proceed showing the next 5 rows?\n").lower()
# this method gets the time travel frequent times
# to get that I used the mode built-in method
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()
# the most common month
print('The most frequent month is: ', df['start_month'].mode()[0])
# the most common day of week
print('The most frequent day is: ', df['start_day'].mode()[0])
# the most common start hour
print('The most commoon start hour is: ', df['start_time'].mode()[0])
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
# in this method I get some statics about the stations of the trip
# used mode and groupby
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()
# most commonly used start station
print('The most commonly used start station is: ', df['Start Station'].mode()[0] )
# most commonly used end station
print('The most commonly used end station is: ', df['End Station'].mode()[0] )
# most frequent combination of start station and end station trip
print('The most frequent combination of start station and end station trip is: ',
df.groupby(['Start Station','End Station']).size().idxmax())
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
# In this method I get some statics about the trip duration
# used the sum, mean aggregation functions
def trip_duration_stats(df):
"""Displays statistics on the total and average trip duration."""
print('\nCalculating Trip Duration...\n')
start_time = time.time()
# total travel time
# the trip duration coulmn is in seconds
# to make it more readable I convert it to days by dividing it on 86400
print('The total travel time in hours is: ', df['Trip Duration'].sum()/86400)
# mean travel time
print('The average travel time in minutes is: ', df['Trip Duration'].mean()/60)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
# In this method I get some statics about the users
# Using
def user_stats(df, city):
"""Displays statistics on bikeshare users."""
print('\nCalculating User Stats...\n')
start_time = time.time()
# counts of user types
print('In this city, we have diffrent types of users as follows: ')
print(df['User Type'].value_counts())
# this condition because the washington csv doens't include gender and year birth coulmns
if city in ('new york city', 'chicago'):
# counts users based on gender
print('The total count of each gender is as follow: ')
print('Females:', df['Gender'].value_counts().get("Female", 0))
print('Males:', df['Gender'].value_counts().get("Male", 0))
print('Unknown:', df['Gender'].value_counts().get("Unknown", 0))
# So because I don't want to include the unknown value of these I will use a filter on the dataset
# earliest year of birth
print('The earliest year of birth is: ', df['Birth Year'].min())
# Something doesn't add up here because it first displays to me the (unknown) so because I used it to fill the missing data
# I am thinking to impute the missing birth year with the mode of it
# but this will effect the time since I already imputed why impute twice
# so what can I do ?
# most recent of birth
print('The most recent year of birth is: ', df['Birth Year'].max())
# most common year of birth
print('The most common year of birth is: ', df['Birth Year'].mode()[0])
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def main():
# start the program until the user hits no ot there exists an exception
try:
while True:
# gets the filters
city, month, day = get_filters()
# load the dataset
df = load_data(city, month, day)
# clean the dataset
# Here I pass the city because in case the city is washington
# coulmns Gender and Birth Year coulmns doesn't exist
df= clean_data(df, city)
# ask the user if they want to print the data
display_data(df)
# Display diffrent statics of the dataset
time_stats(df)
station_stats(df)
trip_duration_stats(df)
# Here I pass the city because in case the city is washington
# coulmns Gender and Birth Year coulmns doesn't exist
user_stats(df, city)
# the user can restart and try diffrent cities if they
# key hit no the program will hault
restart = str(input('\nWould you like to restart? Enter yes or no.\n'))
if restart.lower() != 'yes':
break
# Any exception that occures will be printed and traced
except Exception as e:
print("The program encountered an error: ",
type(e).__name__, " : ", e)
traceback.print_exc()
############################
# In this project the dataset of diffrent city is explored
# by the user interactivly of diffrent cities
############################
if __name__ == "__main__":
main()