In [2]:
import pandas as pd

filename = 'chicago.csv'

# load data file into a dataframe
df = pd.read_csv('data/chicago.csv')
In [4]:
df['Start Time'].head()
Out[4]:
0    2017-05-29 18:36:27
1    2017-06-12 19:00:33
2    2017-02-13 17:02:02
3    2017-04-24 18:39:45
4    2017-01-26 15:36:07
Name: Start Time, dtype: object
In [5]:
# convert the Start Time column to datetime
df['Start Time'] = pd.to_datetime(df['Start Time'])
df['Start Time'].dt.date.head()
Out[5]:
0    2017-05-29
1    2017-06-12
2    2017-02-13
3    2017-04-24
4    2017-01-26
Name: Start Time, dtype: object
In [6]:
df['Start Time'].dt.time.head()
Out[6]:
0    18:36:27
1    19:00:33
2    17:02:02
3    18:39:45
4    15:36:07
Name: Start Time, dtype: object
In [7]:
# extract hour from the Start Time column to create an hour column
df['hour'] = df['Start Time'].dt.hour
df['hour'].head()
Out[7]:
0    18
1    19
2    17
3    18
4    15
Name: hour, dtype: int64
In [8]:
# find the most popular hour
print(df['hour'].mode())
popular_hour = df['hour'].mode()[0]
print(popular_hour)
0    17
dtype: int64
17
In [9]:
# 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)
    hour  count
0      0      4
1      5      2
2      6     14
3      7     23
4      8     22
5      9     20
6     10     16
7     11     19
8     12     31
9     13     26
10    14     21
11    15     25
12    16     29
13    17     53
14    18     36
15    19     26
16    20     11
17    21     11
18    22      7
19    23      4

13

hour     17
count    53
Name: 13, dtype: int64

Most Frequent Start Hour: 17
In [16]:
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
In [13]:
load_data('chicago', 'march', 'friday')
Out[13]:
Start Time End Time Trip Duration Start Station End Station User Type Gender Birth Year month day_of_week
40 2017-03-24 13:06:37 2017-03-24 13:10:44 247 Broadway & Berwyn Ave Clark St & Berwyn Ave Subscriber Female 1961.0 3 Friday
59 2017-03-03 07:55:48 2017-03-03 07:57:41 113 Clark St & Chicago Ave Wells St & Huron St Subscriber Male 1981.0 3 Friday
68 2017-03-17 12:14:50 2017-03-17 12:22:38 468 Dearborn Pkwy & Delaware Pl State St & Randolph St Subscriber Female 1984.0 3 Friday
75 2017-03-10 13:40:54 2017-03-10 13:45:09 255 Clark St & Lake St Rush St & Hubbard St Subscriber Female 1983.0 3 Friday
83 2017-03-24 14:15:43 2017-03-24 14:27:04 681 Sheridan Rd & Lawrence Ave Broadway & Thorndale Ave Subscriber Male 1984.0 3 Friday
126 2017-03-24 12:39:19 2017-03-24 12:52:11 772 Michigan Ave & Oak St Cannon Dr & Fullerton Ave Subscriber Male 1993.0 3 Friday
224 2017-03-31 19:11:12 2017-03-31 19:18:53 461 Damen Ave & Cortland St Damen Ave & Pierce Ave Subscriber Male 1989.0 3 Friday
247 2017-03-10 08:21:05 2017-03-10 08:23:28 143 Damen Ave & Division St Ashland Ave & Division St Subscriber Male 1991.0 3 Friday
290 2017-03-24 10:55:53 2017-03-24 11:01:27 334 Wacker Dr & Washington St LaSalle St & Jackson Blvd Subscriber Male 1961.0 3 Friday
343 2017-03-17 17:51:31 2017-03-17 18:00:16 525 Milwaukee Ave & Grand Ave State St & Pearson St Subscriber Male 1989.0 3 Friday
348 2017-03-31 07:47:14 2017-03-31 07:55:38 504 Canal St & Madison St Wabash Ave & Adams St Subscriber Male 1953.0 3 Friday
In [ ]: