i have hourly data, how to aggregate them to daily with average pandas dataframe in python

If you have a pandas dataframe with hourly data, the following code snippet will help you to aggregate them into the daily data with average values:

main.py
import pandas as pd

# Sample hourly data
data = {'Date': ['1-1-2022', '1-1-2022', '2-1-2022', '2-1-2022'],
        'Value': [10, 20, 30, 40]}
df = pd.DataFrame(data)

# Convert 'Date' column to datetime type
df['Date'] = pd.to_datetime(df['Date'], format="%d-%m-%Y")

# Set 'Date' column as index for resampling
df = df.set_index('Date')

# Resample data to daily frequency and calculate average
daily_df = df.resample('D').mean()

# Optional: reset index
daily_df.reset_index(inplace=True)  
494 chars
19 lines

Here, we first convert the 'Date' column to datetime type using pd.to_datetime() method. Then we set the 'Date' column as the index for resampling the data to daily frequency using resample() method. Finally, we use mean() method to calculate the daily average and store the result in a new dataframe.

Note that the code assumes that your original dataframe has columns named "Date" and "Value". You should modify it accordingly if your dataframe has different column names.

I hope it helps!

related categories

gistlibby LogSnag