import pandas as pd
filename = 'chicago.csv'
# load data file into a dataframe
df = pd.read_csv('data/chicago.csv')
df['Start Time'].head()
# convert the Start Time column to datetime
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['Start Time'].dt.date.head()
df['Start Time'].dt.time.head()
# extract hour from the Start Time column to create an hour column
df['hour'] = df['Start Time'].dt.hour
df['hour'].head()
# find the most popular hour
print(df['hour'].mode())
popular_hour = df['hour'].mode()[0]
print(popular_hour)
# find the most common hour (from 0 to 23)
popular_hour = df.groupby(['hour']).size().reset_index(name='count')
print()
print(popular_hour)
print()
print(popular_hour['count'].idxmax())
print()
print(popular_hour.loc[popular_hour['count'].idxmax()])
print()
print('Most Frequent Start Hour:', popular_hour.loc[popular_hour['count'].idxmax()].hour)
CITY_DATA = { 'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv' }
def load_data(city, month, day):
# load data file into a dataframe
df = pd.read_csv('data/' + 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()]
return df
load_data('chicago', 'march', 'friday')