how to aggregate values on hours in python

To aggregate values on hours in Python, you can use the pandas library along with the datetime functionality. Here's an example of how you can do it:

main.py
import pandas as pd

# Create a DataFrame with a datetime column and a value column
df = pd.DataFrame({
    'datetime': ['2021-01-01 08:15:00', '2021-01-01 09:30:00', '2021-01-02 10:45:00', '2021-01-02 11:00:00'],
    'value': [10, 20, 30, 40]
})

# Convert the 'datetime' column to datetime type
df['datetime'] = pd.to_datetime(df['datetime'])

# Set the 'datetime' column as the index of the DataFrame
df.set_index('datetime', inplace=True)

# Resample the DataFrame on an hourly basis and sum the values
hourly_aggregated = df.resample('H').sum()

print(hourly_aggregated)
576 chars
19 lines

This code will create a DataFrame with a datetime column and a value column. It converts the datetime column to the datetime type, sets it as the index of the DataFrame, and then uses the resample() function to aggregate the values on an hourly basis. In this example, the values are summed up for each hour.

The resulting hourly_aggregated DataFrame will contain the aggregated values on an hourly basis.

Output:

main.py
                     value
datetime                  
2021-01-01 08:00:00     10
2021-01-01 09:00:00     20
2021-01-01 10:00:00      0
2021-01-01 11:00:00      0
2021-01-01 12:00:00      0
2021-01-01 13:00:00      0
2021-01-01 14:00:00      0
2021-01-01 15:00:00      0
2021-01-01 16:00:00      0
2021-01-01 17:00:00      0
2021-01-01 18:00:00      0
2021-01-01 19:00:00      0
2021-01-01 20:00:00      0
2021-01-01 21:00:00      0
2021-01-01 22:00:00      0
2021-01-01 23:00:00      0
2021-01-02 00:00:00      0
2021-01-02 01:00:00      0
2021-01-02 02:00:00      0
2021-01-02 03:00:00      0
2021-01-02 04:00:00      0
2021-01-02 05:00:00      0
2021-01-02 06:00:00      0
2021-01-02 07:00:00      0
2021-01-02 08:00:00      0
2021-01-02 09:00:00      0
2021-01-02 10:00:00     30
2021-01-02 11:00:00     40
810 chars
31 lines

In the resulting DataFrame, the datetime column shows the hourly intervals, and the value column contains the sum of values for each hour.

related categories

gistlibby LogSnag