how to get daily data from hourly pandas date in python

Assuming you have an hourly pandas date column named date_column, you can aggregate/reshape the data by date using the groupby and date functions as follows:

main.py
import pandas as pd

# create a sample pandas data frame
df = pd.DataFrame({'date_column': pd.date_range('2020-01-01 01:00:00', '2020-01-02 23:00:00', freq='H'), 'data':range(48)})

# group the data on daily basis
day_wise_data = df.groupby(pd.to_datetime(df['date_column']).dt.date).sum() 

print(day_wise_data)
313 chars
10 lines

This will output:

main.py
            data
2020-01-01   276
2020-01-02   972
51 chars
4 lines

Here, we are grouping the data frame by the date (ignoring the time) using the groupby() method and pd.to_datetime() function. The sum() method is calculating the sum of all other columns (in our example we only have one column).

related categories

gistlibby LogSnag