-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUS_METRO.py
352 lines (257 loc) · 9.36 KB
/
US_METRO.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
340
341
342
343
344
345
346
347
348
349
350
351
352
import time
import pandas as pd
import numpy as np
import datetime as dt
CITY_DATA = { 'chicago': 'chicago.csv',
'new york city': '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
"""
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
print('\n')
i=1
print('Hi my name is Abhishek.\n Iam here to help you with US Metro data.\n')
while i>0:
city = (input("For which city do u want to look data for?\n(1.)Chicago\n(2.)New York\n(3.)Washington\n\n")).lower()
if city == "chicago":
a='chicago.csv'
i=0
elif city == 'new york':
a= 'new_york_city.csv'
i=0
elif city == 'washington':
a= 'washington.csv'
i=0
else:
print('Wrong Input')
i=1
# get user input for month (all, january, february, ... , june)
print('Now Enetr the name of the month for which you want to see the data\n')
print('Choose from the following:\n')
print('all,January, February, March, April, May, or June')
print("Enetr your choice:\n")
i=1
while i>0:
month=input(":-")
if month == 'january':
b= 1
i=0
elif month == 'february':
b= 2
i=0
elif month == 'march':
b= 3
i=0
elif month == 'april':
b= 4
i=0
elif month == 'may':
b= 5
i=0
elif month == 'june':
b= 6
i=0
elif month == 'all':
b= 7
i=0
else:
print('Wrong choice try again')
i=1
# get user input for day of week (all, monday, tuesday, ... sunday)
print('Now Enter the day of the week for which you are looking for\n')
print('Choose from :\n')
print('Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday or all\n')
i=1
while i!=0:
day =(input(":-")).lower()
if day == 'monday':
c= 1
i=0
elif day == 'tuesday':
c= 2
i=0
elif day == 'wednesday':
c= 3
i=0
elif day == 'thursday':
c= 4
i=0
elif day == 'friday':
c= 5
i=0
elif day == 'saturday':
c= 6
i=0
elif day == 'sunday':
c= 7
i=0
elif day == 'all':
c= 8
i=0
else:
print('wrong choice .Try again\n')
i=1
print('-'*40)
return a,b,c
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=pd.read_csv(city)
city['Start Time'] = pd.to_datetime(city['Start Time'])
city['month']=city['Start Time'].dt.month
city['day']=city['Start Time'].dt.weekday_name
days =['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
if month != 7:
b=city[city['month'] == month]
else:
b=city
if day !=8:
df=b[city['day'] == days[day-1]]
else:
df=b
return df,b
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
m2=['January','February','March','April','May','June']
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['month'] = df['Start Time'].dt.month
popular_month =[df['month'].mode()[0]]
print('\n\nThe most common month is\n')
print(":-")
print(popular_month)
# display the most common day of week
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['day'] = df['Start Time'].dt.dayofweek
popular_day = df['day'].mode()[0]
print('\n\nThe most common day of the week is\n')
print(":-")
print(popular_day)
# 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('\n\nThe most common start hour is\n')
print(":-")
print(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()
# display most commonly used start station
d=df.groupby('Start Station')['Start Station'].count()
d1=d.sort_values(ascending=False)
print('The most popular Start Station is:- {}'.format(d1.index[0]))
print('\n\n')
# display most commonly used end station
e=df.groupby('End Station')['End Station'].count()
e1=e.sort_values(ascending=False)
print('The most popular End staion is:- {}'.format(e1.index[0]))
print('\n\n')
# display most frequent combination of start station and end station trip
f=df.groupby(['Start Station','End Station'])['Start Station'].count()
f1=f.sort_values(ascending=False)
print('The most frequent combination of start station and end station is:- {} and {}'.format(f1.index[0][0],f1.index[0][1]))
print('\n\n')
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
t1=df['Trip Duration'].sum()
t2=df['Trip Duration'].mean()
mi , sec=divmod(t1,60)
hou , mi=divmod(mi,60)
date,hou=divmod(hou,24)
year,date=divmod(date,365)
print(" the total trip duration is :- {} years {}days {}hours {}minutes {}seconds".format(year,date,hou,mi,sec))
print('\n\n\n')
# display mean travel time
mi,sec=divmod(t2,60)
hou, mi=divmod(mi,60)
date,hou=divmod(hou,24)
year,date=divmod(date,365)
print(" the avaerage trip duration is :- {} years {}days {}hours {}minutes {}seconds".format(year,date,hou,mi,sec))
print('\n\n\n')
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()
# Display counts of user types
user_types = df['User Type'].value_counts()
print('The counts of user types is:\n')
print(user_types)
print('\n\n')
# Display counts of gender
if 'Gender' in df.columns:
gender_count=df['Gender'].value_counts()
print('The counts of gender is:\n')
print(gender_count)
print('\n\n')
else:
print("No gender values present\n")
# Display earliest, most recent, and most common year of birth
dobearliest=int(df['Birth Year'].min())
print("The earliest birth year is : {}\n".format(dobearliest))
dobrecent=int(df['Birth Year'].max())
print('The most recent birth year is : {}\n'.format(dobrecent))
dobcommon=int(df['Birth Year'].mode()[0])
print("the most common birth year is : {}\n".format(dobcommon))
print('\n\n')
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def main():
while True:
city, month, day = get_filters()
df,df1 = load_data(city, month, day)
i=0
print("Do you want to look at first 5 rows of the dataset\n")
print("Yes[Y] or No[N]\n\n")
dic=input().lower()
a1=0
b1=5
if dic =='n':
else:
while i<1:
print(df1.iloc[a1:b1])
print('\n\n')
print("Do you want to print next 5 rows of dataset again\n")
print("Yes[Y] or No[N]\n")
dic2=input().lower()
if dic2 =='n':
i=1
else:
a1=b1
b1+=5
i=0
time_stats(df1)
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
if __name__ == "__main__":
main()