Title: Group Pandas Data By Hour Of The Day
Slug: group_pandas_data_by_hour_of_the_day
Summary: Group data by hour of the day using pandas.
Date: 2016-12-21 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon
In [1]:
# Import libraries
import pandas as pd
import numpy as np
In [2]:
# Create a time series of 2000 elements, one very five minutes starting on 1/1/2000
time = pd.date_range('1/1/2000', periods=2000, freq='5min')
# Create a pandas series with a random values between 0 and 100, using 'time' as the index
series = pd.Series(np.random.randint(100, size=2000), index=time)
In [3]:
# View the first few rows of the data
series[0:10]
Out[3]:
In [4]:
# Group the data by the index's hour value, then aggregate by the average
series.groupby(series.index.hour).mean()
Out[4]: