Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time as 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 city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
- while True:
- city = input('Please choose a city (Chicago, New York City, Washington):\n').lower()
- if city not in ('chicago', 'new york city', 'washington'):
- print('Wrong input! Please choose a city (Chicago, New York City, Washington):\n')
- continue
- else:
- break
- # get user input for month (all, january, february, ... , june)
- while True:
- month = input('Please choose a month (January, February, March, April, May, June or all:\n').lower()
- if month not in ('january', 'february', 'march', 'april', 'may', 'june', 'all'):
- print('Wrong input! Please choose a month (January, February, March, April, May, June or all:\n')
- continue
- else:
- break
- # get user input for day of week (all, monday, tuesday, ... sunday)
- while True:
- day = input('Please choose a day (Monday, Tuesday, ... Sunday or all:\n').lower()
- if day not in ('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'all'):
- print('Wrong input! Please choose a day (Monday, Tuesday, ... Sunday or all:\n')
- continue
- 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])
- df['Start Time'] = pd.to_datetime(df['Start Time'])
- df['month'] = df['Start Time'].dt.month
- df['day_of_week'] = df['Start Time'].dt.weekday_name
- df['hour'] = df['Start time'].dt.hour
- if months != 'all':
- months = ['january', 'february', 'march', 'april', 'may', 'june']
- month = months.index(month) + 1
- df = df[df['month'] == month]
- if day != 'all':
- df = df[df['day_of_week'] == day.title()]
- 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()
- # display the most common month
- popular_month = df['month'].mode()[0] - 1
- print('The most popular month is {}.'.format(months[popular_month].title()))
- # display the most common day of week
- popular_day = df['day_of_week'].mode()[0]
- print('The most popular day of week is {}.'.format(popular_day))
- # display the most common start hour
- popular_hour = df['hour'].mode()[0]
- print('The most popular hour is {}.'.format(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
- popular_start_station = df['Start Station'].mode()[0]
- print('{} is the most popular start station.'.format(popular_start_station))
- # display most commonly used end station
- popular_end_station = df['End Station'].mode()[0]
- print('{} is the most popular end station.'.format(popular_end_station))
- # display most frequent combination of start station and end station trip
- station_pair = df.groupby(['Start Station', 'End Station']).size().nlargest(1).reset_index(name='count')
- print('The most frequent combination of stations is {} (start) and {} (end).\nThis route was used {} time(s).'.format(station_pair['Start Station'][0], station_pair['End Station'][0], station_pair['count'][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."""
- print('\nCalculating Trip Duration...\n')
- start_time = time.time()
- df['End Time'] = pd.to_datetime(df['End Time'])
- df['Trip Duration'] = df['End Time'] - df['Start Time']
- # display total travel time
- total_travel_time = df['Trip Duration'].sum()
- print('Total travel time of all bike rides is {}.'.format(total_travel_time))
- # display mean travel time
- mean_travel_time = df['Trip Duration'].mean()
- print('Mean travel time is {}.'.format(mean_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
- user_type_count = df['User Type'].value_counts()
- print('User counts:\n', user_type_count)
- # Display counts of gender
- gender_count = df['Gender'].value_counts()
- print('Gender counts:\n', gender_count)
- # Display earliest, most recent, and most common year of birth
- min_year = df['Birth Year'].min()
- print('The earliest year of birth is {}.'.format(min_year))
- max_year = df['Birth Year'].max()
- print('The most recent year of birth is {}.'.format(max_year))
- common_year = df['Birth Year'].mode()[0]
- print('The most common year of birth is {}.'.format(common_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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement