forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bike_share_sys_project3.py
178 lines (139 loc) · 6.65 KB
/
Bike_share_sys_project3.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
import time
import datetime as dt
import pandas as pd
import numpy as np
from pyfiglet import Figlet
from colorama import init
from termcolor import colored
# use Colorama to make Termcolor work on Windows too
init()
####
welcome_text = Figlet(font='slant')
print(welcome_text.renderText('Explore The US Bike Share Data'))
###
CITY_DATA = { 'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv' }
def get_filters():
print(colored('Hello! Let\'s explore some US bikeshare data!', 'blue'))
# get user input for city (chicago, new york city, washington).
while True:
city = input("Would you like to see data for Chicago, New York city, or Washington?").lower()
if city.lower() not in ('chicago', 'new york city', 'washington'):
print(colored('\nNot quiet a valid input, try again!','red'))
else:
break
while True:
month = input("Which month? -January, February, March, April, May, June, or type 'all'").lower()
if month.lower() not in ('january', 'february', 'march', 'april', 'may', 'june', 'all'):
print(colored('\nNot quiet a valid input, try again!','red'))
else:
break
while True:
day = input("Which day? Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, or type 'all'").lower()
if day.lower() not in ('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'all'):
print(colored('\nNot quiet a valid input, try again!','red'))
else:
break
print('-'*40)
return city, month, day
def load_data(city, month, day):
df = pd.read_csv(CITY_DATA[city])
df['Start Time'] = pd.to_datetime(df['Start Time']) #change object type
df['day'] = df['Start Time'].dt.day_name() #day column
df['month'] = df['Start Time'].dt.month #month column
#filter by month
if month != 'all':
months = ['january', 'february', 'march', 'april', 'may', 'june']
month = months.index(month) +1
df = df[df['month'] == month]
#filter by day
if day != 'all':
df = df[df['day'] == day.title()]
return df
def time_stats(df):
print(colored('\nCalculating The Most Frequent Times of Travel...\n','green'))
start_time = time.time()
# display the most common month
most_common_month = df['month'].mode()[0]
# display the most common day of week
most_common_day = df['day'].mode()[0]
# display the most common start hour
df['hour'] = df['Start Time'].dt.hour #hour column
most_common_hour = df['hour'].mode()[0]
print(colored('These are some statistics regarding travel time: \n the most popular month in which bikes were shared was {}.\n the most popular day of the week in which bikes were shared was {}. \n the most popular hour in which bikes were shared was {}.','grey','on_white').format(most_common_month, most_common_day,most_common_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(colored('\nCalculating The Most Popular Stations and Trip...\n', 'green'))
start_time = time.time()
# display most commonly used start station
common_start_station = df['Start Station'].mode()[0]
# display most commonly used end station
common_end_station = df['End Station'].mode()[0]
# display most frequent combination of start station and end station trip
most_frequent_station_combination = df.value_counts(['Start Station', 'End Station']).index.tolist()[0]
print(colored('\nThe most popular start station was {}.\nThe most popular end station was {}. \nWhile the most frequent combination of start and end stations was {}.','grey','on_white').format(common_start_station, common_end_station, most_frequent_station_combination))
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(colored('\nCalculating Trip Duration...\n','green'))
start_time = time.time()
# display total travel time
total_travel_time = int(df['Trip Duration'].sum())
total =dt.timedelta(seconds = total_travel_time)
# display mean travel time
mean_of_travel_time = df['Trip Duration'].mean()
mean = dt.timedelta(seconds = mean_of_travel_time)
print(colored("\nTotal travel time 'in days' was {}.\nThe mean of travel durations was {}",'grey','on_white').format(total, mean))
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def user_stats(df):
"""Displays statistics on bikeshare users."""
print(colored('\nCalculating User Stats...\n','green'))
start_time = time.time()
# Display counts of user types
all_users = df['User Type'].value_counts()
print('\nUser Stats:\n', all_users)
# Display counts of gender
if 'Gender'in df:
gender_count = df['Gender'].value_counts()
print('\nGenders:\n', gender_count)
# Display earliest, most recent, and most common year of birth
if 'Birth Year' in df:
most_common_birth_year = df['Birth Year'].mode()[0]
most_recent_birth_year = df['Birth Year'].min()
earliest_birth_year = df['Birth Year'].max()
print(colored('\nThe most_common_birth_year is {}. \nThe youngest user was born in {}.\nWhile the oldest user was born in {}.','grey','on_white').format(most_common_birth_year, earliest_birth_year, most_recent_birth_year ))
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
#row data
def display_data(df):
while True:
view_data = input('\nWould you like to view 5 rows of individual trip data? Enter yes or no\n')
if view_data.lower() not in ('yes', 'no', 'n', 'y', 'Yes', 'No', 'YES','NO'):
print('\nNot a valid input, please try again with yes or no input')
else:
start_loc = 0
while view_data != 'no':
print(df.iloc[start_loc:start_loc+5])
start_loc += 5
view_display = input("\nDo you wish to continue?: Enter yes or no.\n").lower()
if view_display != 'yes':
break
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)
display_data(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()