-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBikeShare_Udacity
More file actions
180 lines (157 loc) · 5.77 KB
/
BikeShare_Udacity
File metadata and controls
180 lines (157 loc) · 5.77 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
import pandas as pd
import numpy as np
import time
data_ = { 'chicago': 'chicago.csv',
'newyork': 'new_york_city.csv',
'washington': 'washington.csv' }
def pull_month():
month_option=['January','February','March','April','May','June','July','August','September','October','November','December']
while True:
month = input('Which month you wanna want')
month = month.lower()
if(month in month_option):
break
return month
def pull_day():
days_option=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
while True:
day = input('Which day you wanna want?')
day =d ay.lower()
if(day in days_option):
break
return day
def pull_filter():
print('Can we start searching the bike share data')
city_option=['Chicago','Newyork','Washington']
while True:
city = input('Choose One from the options visible Chicago, Newyork, Washington')
city = city.lower()
if(city in city_option):
break
while True:
filter_option = [1,2,3,4]
filter = input('Would you like to filter using month:1 day:2 both:3 nothing:4 enter 1, 2, 3, or 4 ')
filter = int(filter)
if(filter in filter_option):
break
if(filter == 1):
month=pull_month()
day='all'
elif(filter == 2):
day=pull_day()
month='all'
elif(filter == 3):
month=pull_month()
day=pull_day()
elif(filter == 4):
day='all'
month='all'
print('-'*40)
return city, month, day
def load_data(city, month, day):
df = pd.read_csv(data_[city])
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['month'] = df['Start Time'].dt.month
df['day'] = df['Start Time'].dt.weekday_name
if month != 'all':
months = ['January', 'February', 'March', 'April', 'May', 'June','July','August','September','October','November','December']
month = months.index(month)+1
df = df[df['month'] == month]
if day != 'all':
df = df[df['day'] == day.title()]
return df
def common_month(df):
df['month'] = df['Start Time'].dt.month_name()
popular_month=df['month'].mode()[0]
print('Popular Start Month:',popular_month)
def common_day(df):
df['day'] = df['Start Time'].dt.day_name()
popular_day=df['day'].mode()[0]
print('Popular Day:',popular_day)
def common_start_hour(df):
df['hour'] = df['Start Time'].dt.hour
popular_hour=df['hour'].mode()[0]
print('Popular Start Hour:',popular_hour)
def time_stats(df, month, day):
print('Most Frequent Times of Travel')
start_time = time.time()
if(month=='all' and day=='all'):
common_month(df)
common_day(df)
common_start_hour(df)
elif(month!='all' and day == 'all'):
common_day(df)
common_start_hour(df)
elif(month=='all' and day!='all'):
common_start_hour(df)
common_month(df)
elif(month!='all' and day!='all'):
common_start_hour(df)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def station_stats(df):
print('Most Popular Stations and Trip')
start_time = time.time()
popular_start_station=df['Start Station'].mode()[0]
print('Popular Start Station:', popular_start_station)
popular_end_station=df['End Station'].mode()[0]
print('Popular End Station:',popular_end_station)
df['Start End'] = df['Start Station'].map(str) + '&' + df['End Station']
popular_start_end = df['Start End'].value_counts().idxmax()
print('Most Popular frequent combination of start station and end station is',popular_start_end)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def trip_duration_stats(df):
print('\nCalculating Trip Duration...\n')
start_time = time.time()
total_travel_time=df['Trip Duration'].sum()
print('Total Travel Time:',total_travel_time)
mean_travel_time=df['Trip Duration'].mean()
print('Mean Travel Time:',mean_travel_time)
print("This took %s seconds." % (time.time() - start_time))
print('-'*40)
def user_stats(df,city):
print('Calculating User Stats')
start_time = time.time()
user_types = df['User Type'].value_counts()
print('User Type',user_types)
if city=='newyork' or city=='chicago':
gender_types = df['Gender'].value_counts()
print('Gender Type:',gender_types)
earliest_year_birth = int(df['Birth Year'].min())
print('Earliest Year Birth: ',earliest_year_birth)
most_recent_year_birth =int(df['Birth Year'].max())
print('Most Recent Year Birth: ',most_recent_year_birth)
most_common_year_birth = int(df['Birth Year'].mode()[0])
print('Most common Year Birth: ',most_common_year_birth)
print(" %s seconds" % (time.time() - start_time))
print('-'*40)
def main():
while True:
city, month, day = pull_filter()
load_data(city, month, day)
df = load_data(city, month, day)
time_stats(df, month, day)
station_stats(df)
trip_duration_stats(df)
user_stats(df,city)
flag=1
start=0
end=5
while(flag ==1):
flag=int(input("Individual trip data? Type 1 or 2 True:1 False:2'))
while((flag != 1) and (flag!=2)):
flag=int(input('\nPlease enter available input: '))
print(df.iloc[start:end])
start+=5
end+=5
restart = input('do u wanna do again Enter yes or no')
restart = restart.lower()
while((restart != 'yes') and (restart !='no')):
restart=input('input: ')
if restart == 'no':
break
elif restart =='yes':
continue
if __name__ == "__main__":
main()