forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bikeshare.py
214 lines (160 loc) · 8.02 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
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():
"""
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 month (all, january, february, ... , june)
while True:
city = input('Enter the name of the city of your interest among chicago, new york city and washington:\n').lower().strip()
if city not in ['chicago','new york city','washington']:
print('\nname not valid\n')
else:
break
# Get user input for month (all, january, february, ... , june)
while True:
month = input("Enter the name of the month of your interest from january to june (digit 'all' for data relating to all months):\n").lower().strip()
if month not in ['all','january','february','march','april','may','june']:
print('\nname not valid\n')
else:
break
# Get user input for day of week (all, monday, tuesday, ... sunday)
while True:
day = input("Enter the name of the day of the week of your interest (digit 'all' for data relating to all days):\n").lower().strip()
if day not in ['all','monday','thursday','wednesday','thursday','friday','saturday','sunday']:
print('\nname not valid\n')
else:
break
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])
# convert the Start Time column to datetime
df['Start Time'] = pd.to_datetime(df['Start Time'])
# extract month and day of week from Start Time to create new columns
df['month'] = df['Start Time'].dt.month
df['day_of_week'] = df['Start Time'].dt.weekday_name
# filter by month if applicable
if month != 'all':
# use the index of the months list to get the corresponding int
months = ['january', 'february', 'march', 'april', 'may', 'june']
month = months.index(month) + 1
# filter by month to create the new dataframe
df = df[df['month'] == month]
# filter by day of week if applicable
if day != 'all':
# filter by day of week to create the new dataframe
df = df[df['day_of_week'] == day.title()]
answer=input('do you want to see raw data in batches of five rows? type yes if you want').lower().strip()
i=0
while answer=='yes':
print(df[i:i+5])
answer=input('type yes to see other 5 rows of data').lower().strip()
i+=5
if i> len(df):
break
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()
df['Start Time'] = pd.to_datetime(df['Start Time'])
# Display the most common month
most_common_month = df['Start Time'].dt.month.mode()[0]
count_month= df['Start Time'].dt.month.value_counts()[most_common_month]
print('the most common month is {} with a number of times of {}'.format(most_common_month, count_month))
# Display the most common day of week
most_common_day = df['Start Time'].dt.day.mode()[0]
count_day= df['Start Time'].dt.day.value_counts()[most_common_day]
print('the most common day is {} with a number of times of {}'.format(most_common_day, count_day))
# Display the most common start hour
most_common_start_hour = df['Start Time'].dt.hour.mode()[0]
count_hour=df['Start Time'].dt.hour.value_counts()[most_common_start_hour]
print('the most common start hour is {} with a number of times of {}'.format(most_common_start_hour, count_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
max_1=df['Start Station'].value_counts().max()
most_commonly_used_start_station = df['Start Station'].value_counts()[df['Start Station'].value_counts()==max_1].index[0]
print('the most common used Start Station is {} with a number of times of {}'.format(most_commonly_used_start_station, max_1))
# Display most commonly used end station
max_2=df['End Station'].value_counts().max()
most_commonly_used_end_station = df['End Station'].value_counts()[df['End Station'].value_counts()==max_2].index[0]
print('the most common used End Station is {} with a number of times of {}'.format(most_commonly_used_end_station, max_2))
# Display most frequent combination of start station and end station trip
df['combination']=df['Start Station']+ '-' +df['End Station']
max_3=df['combination'].value_counts().max()
most_frequent_combination = df['combination'].value_counts()[df['combination'].value_counts()==max_3].index[0]
print('the most frequent combination is {} with a number of times of {}'.format(most_frequent_combination, max_3))
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_time = df['Trip Duration'].sum()
print('the total travel time is: {}'.format(total_travel_time))
# Display mean travel time
average_travel_time = df['Trip Duration'].mean(axis=0)
print('the average travel time is: {}'.format(average_travel_time))
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
print( 'The number of users per each type is \n{}'.format(df.groupby(['User Type'])['Unnamed: 0'].count()))
# Display counts of gender
try:
print( 'The number of users per gender is \n{}'.format(df.groupby(['Gender'])['Unnamed: 0'].count()))
except:
print('For the city of Washington there aren\' t informations regarding the users gender')
# Display earliest, most recent, and most common year of birth
try:
earliest_year_of_birth = df['Birth Year'].min()
print('The earliest year of birth is {}'.format(earliest_year_of_birth))
most_recent_year_of_birth = df['Birth Year'].max()
print('The most recent year of birth is {}'.format(most_recent_year_of_birth))
most_common_year_of_birth = df['Birth Year'].mode()[0]
print('The most common year of birth is {}'.format(most_common_year_of_birth))
except:
print('For the city of Washington there aren\' t informations regarding the users birth year')
print("\nThis took %s seconds." % (time.time() - start_time))
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
if __name__ == "__main__":
main()