forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbikeshare.py
299 lines (220 loc) · 10.1 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
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
import time
import pandas as pd
import numpy as np
# Dictionary contains city and its corresponding csv file name
CITY_DATA = {'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv'}
<<<<<<< HEAD
myName="Nasser ALabdullah"
# this function is to select the filters to be applied
||||||| 237c238
# this function is to select the filters to be applied
=======
#this change is made for github project to add commit .
Myname ="Nasser"
# this function is to select the filters to be applied it's very important
>>>>>>> documentation
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 Take the inputs from the user
city = input("\nPlease enter a city from the following: 1.chicago 2.new york city 3.washington\n").lower()
# while loop to make sure that the user entered the correct name of city (option)
while (city not in CITY_DATA.keys()):
city = input("\nPlease enter a city from the following: 1.chicago 2.new york city 3.washington\n").lower()
# TO DO: get user input for month (all, january, february, ... , june)
#
months = ['All', 'January', 'February', 'March', 'April', 'May', 'June']
month = input(
'\nPlease enter month from the folloiwng to filter by:\n\njanuary, february, march, april, may, june\n\nnote:type "all" for no month filtering\n').title()
# while loop to make sure that the user entered the correct name of month
while (month not in months):
month = input(
'Please enter month from the folloiwng to filter by\njanuary, february, march, april, may, june\ntype "all" for no month filtering\n').title()
# TO DO: get user input for day of week (all, monday, tuesday, ... sunday)
days = ['All', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
day = input('\nPlease enter day of the week to filter by\ntype "all" for no day filtering\n').title()
# while loop to make sure that the user entered the day name in the correct way
while (day not in days):
day = input('\nPlease enter a correct day of the week to filter by\ntype "all" for no day filtering\n').title()
print('\nyour choice was :\ncity: {}\nmonth: {}\nday: {}\n'.format(city, month, day))
print('-' * 40)
return city, month, day
# Function to load the selected csv file
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
"""
# creating data frame from csv
df = pd.read_csv(CITY_DATA[city])
# converting Start Time row into datetime data type
df['Start Time'] = pd.to_datetime(df['Start Time'])
# Extracting month and week day from 'Start Time' row
df['Month'] = df['Start Time'].dt.month_name()
df['Day of Week'] = df['Start Time'].dt.day_name()
# filter by month
if month != 'All':
df = df[df['Month'] == month]
# filter by day
if day != 'All':
df = df[df['Day of Week'] == day.title()]
# Returns the selected file as a dataframe (df) with relevant columns
return df
def time_stats(df):
"""Displays statistics on the most frequent times of travel.
Args: df : the data frame
Returns: Nothing
"""
print('\nCalculating The Most Frequent Times of Travel...\n')
start_time = time.time()
# TO DO: display the most common month
# displaying Most common month using pandas mode() method
print('\nMost common month is: ', df['Month'].mode()[0])
# TO DO: display the most common day of week
# displaying Most common day using pandas mode() method
print('\nMost common day is: ', df['Day of Week'].mode()[0])
# TO DO: display the most common start hour
# Extracting Hour column from 'Start Time' column
df['Hour'] = df['Start Time'].dt.hour
# displaying Most common start Hour using pandas mode() method
hr = df['Hour'].mode()[0]
# to convert 24 hour format into 12 hour format
if hr <= 12:
print('\nMost common start hour is: {} AM'.format(hr))
else:
print('\nMost common start hour is: {} PM'.format(hr % 12))
print("\nThis took %s seconds." % (time.time() - start_time))
print('-' * 40)
def station_stats(df):
"""Displays statistics on the most popular stations and trip.
Args: df : the data frame
Returns: Nothing
"""
print('\nCalculating The Most Popular Stations and Trip...\n')
start_time = time.time()
# TO DO: display most commonly used start station
# displaying Most commonly used start station using pandas mode() method
print('\nMost commonly used start station is: ', df['Start Station'].mode()[0])
# TO DO: display most commonly used end station
# displaying Most commonly end station using pandas mode() method
print('\nMost commonly end station is: ', df['End Station'].mode()[0])
# TO DO: display most frequent combination of start station and end station trip
# Creating a new calculated column for 'Start & End' stations
df['Start & End'] = df['Start Station'].str.cat(df['End Station'], sep=' --> ')
# displaying Most frequent combination of start station and end station trip using pandas mode() method
print('\nMost frequent combination of start station and end station trip is: ', df['Start & End'].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.
Args: df : the data frame
Returns: Nothing
"""
print('\nCalculating Trip Duration...\n')
start_time = time.time()
# TO DO: display total travel time
# displaying total travel time using sum() method
print('\nTotal travel duration is: ', df['Trip Duration'].sum())
# TO DO: display mean travel time
# displaying average travel time using mean() method
print('\nAverage travel duration is: ', df['Trip Duration'].mean())
# extra statistics
# what is the largest and smallest duration of travel time
print('\nLargest travel duration is: ', df['Trip Duration'].max())
print('\nSmallest travel duration is: ', df['Trip Duration'].min())
print("\nThis took %s seconds." % (time.time() - start_time))
print('-' * 40)
def user_stats(df):
"""Displays statistics on bikeshare users.
Args: df : the data frame
Returns: Nothing
"""
print('\nCalculating User Stats...\n')
start_time = time.time()
# TO DO: Display counts of user types
# displaying counts of user types using value_counts() method
print('\nCounts of user types: \n', df['User Type'].value_counts())
# TO DO: Display counts of gender
# displaying counts of gender using value_counts() method
# handling any error would show up because 'washington' csv file has no Gender column
try:
print('\nCounts of gender: \n', df['Gender'].value_counts())
except:
print('\nSorry, Whasington has no "Gender" informations')
# TO DO: Display earliest, most recent, and most common year of birth
# displaying earliest, most recent, and most common year of birth
# handling any error would show up because 'washington' csv file has no Birth Year column
try:
oldest = int(df['Birth Year'].min())
youngest = int(df['Birth Year'].max())
most = int(df['Birth Year'].mode())
print('\nOldest User/Customer year of birth is: ', oldest)
print('\nYoungest User/Customer year of birth is: ', youngest)
print('\nMost common User/Customer year of birth is: ', most)
except:
print('\nSorry, Whasington has no "year of birth" informations')
print("\nThis took %s seconds." % (time.time() - start_time))
print('-' * 40)
def display_raw_data(city):
"""Show 5 records from the selected city.
Asks user to type if he wants to show raw data or not
Args:
(df): the data frame of the selected city.
Returns:
Nothing.
"""
df = pd.read_csv(CITY_DATA[city])
answers = ['no','yes']
user_input = ''
#counter to use later in displaying raw data with df.head() method
i = 0
#if the user wants to see more records
while user_input not in answers:
print("\nDo you wanna see raw data records?")
print("\nPlease type: Yes or No\n")
user_input = input().lower()
#displaying 5 records of raw data if user says yes using df.head() method
if user_input == "yes":
print(df.head())
elif user_input not in answers:
print("\nPlease enter a right answer.")
#Another loop to ask the user if he wants more data to be displayed
while user_input == 'yes':
print("\nDo you wanna see MORE raw data records?\n")
i += 5
user_input = input().lower()
#If yes -> display more 5 records, else -> break
if user_input == "yes":
print(df[i:i+5])
elif user_input != "yes":
break
print('-'*40)
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)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
display_raw_data(city)
#Main function to call all the previous functions
if __name__ == "__main__":
main()