import time import pandas as pd import numpy as np import json CITY_DATA = {'chicago': 'chicago.csv', 'new york city': 'new_york_city.csv', 'washington': 'washington.csv'} MONTHS = ['january', 'february', 'march', 'april', 'may', 'june'] DAYS = ['sunday', 'monday', 'tuesday', 'wednesday', \ 'thursday', 'friday', 'saturday'] 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('Which of these cities do you want to explore : Chicago, New York or Washington? \n> ').lower() if city in cities: break while True: month = input("Select month: ").lower() if month in MONTHS: break while True: day = input("Select day: ").lower() if day in DAYS: 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 """ # load data file into a dataframe 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 and hour from Start Time to create new columns df['month'] = df['Start Time'].dt.month df['day_of_week'] = df['Start Time'].dt.day_name() df['hour'] = df['Start Time'].dt.hour # filter by month if applicable if month != 'all': month = MONTHS.index(month) + 1 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()] 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 most_common_month = df['month'].value_counts().idxmax() print("The most common month is :", most_common_month) # display the most common day of week most_common_day_of_week = df['day_of_week'].value_counts().idxmax() print("The most common day of week is :", most_common_day_of_week) # display the most common start hour most_common_start_hour = df['hour'].value_counts().idxmax() print("The most common start hour is :", most_common_start_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 most_common_start_station = df['Start Station'].value_counts().idxmax() print("The most commonly used start station :", most_common_start_station) # display most commonly used end station most_common_end_station = df['End Station'].value_counts().idxmax() print("The most commonly used end station :", most_common_end_station) # display most frequent combination of start station and end station trip most_common_start_end_station = df.groupby(['Start Station', 'End Station']).size().idxmax() print("The most commonly used start station and end station : {}, {}" \ .format(most_common_start_end_station[0], most_common_start_end_station[1])) 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 = df['Trip Duration'].sum() print("Total travel time :", total_travel) # display mean travel time mean_travel = df['Trip Duration'].mean() print("Mean travel time :", mean_travel) 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_counts = df['User Type'].value_counts() for index, user_count in enumerate(user_counts): print(" {}: {}".format(user_counts.index[index], user_count)) # Display counts of gender try: print("Counts of gender:\n") gender_counts = df['Gender'].value_counts() for index, gender_count in enumerate(gender_counts): print(" {}: {}".format(gender_counts.index[index], gender_count)) except: print("Dataset doesn't have Gender column") # Display earliest, most recent, and most common year of birth try: birth_year = df['Birth Year'] most_common_year = birth_year.value_counts().idxmax() print("The most common birth year:", most_common_year) most_recent = birth_year.max() print("The most recent birth year:", most_recent) earliest_year = birth_year.min() print("The most earliest birth year:", earliest_year) except: print("Dataset doesn't have Birth Year column") print("\nThis took %s seconds." % (time.time() - start_time)) print('-' * 40) def display_data(df): """Displays raw bikeshare data.""" row_length = df.shape[0] for i in range(0, row_length, 5): yes = input('\nWould you like to view 5 rows of individual trip data ? Enter yes or no\n') if yes.lower() != 'yes': break row_data = df.iloc[i: i + 5] print(row_data) 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()
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |