0
0
Pandasdata~30 mins

Resampling time series data in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Resampling Time Series Data
📖 Scenario: You work as a data analyst for a weather station. You have collected temperature data every hour for a few days. Now, you want to analyze the data by looking at daily average temperatures instead of hourly values.
🎯 Goal: Learn how to resample hourly time series data to daily averages using pandas.
📋 What You'll Learn
Create a pandas DataFrame with hourly temperature data and datetime index
Create a variable for the resampling frequency
Use pandas resample method to calculate daily average temperatures
Print the resulting daily average temperature DataFrame
💡 Why This Matters
🌍 Real World
Weather stations and many industries collect data frequently. Resampling helps summarize and analyze data at different time scales.
💼 Career
Data analysts and scientists often resample time series data to find trends and patterns over days, weeks, or months.
Progress0 / 4 steps
1
Create hourly temperature data
Create a pandas DataFrame called df with a datetime index from '2024-01-01 00:00' to '2024-01-03 23:00' hourly. Add a column 'temperature' with these exact values: [22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1].
Pandas
Need a hint?

Use pd.date_range to create the datetime index with hourly frequency. Then create the DataFrame with the temperature column.

2
Set resampling frequency
Create a variable called freq and set it to the string 'D' to represent daily frequency for resampling.
Pandas
Need a hint?

Set freq to the string 'D' which means daily frequency in pandas.

3
Resample to daily average temperature
Create a new DataFrame called daily_avg by resampling df using the freq variable and calculating the mean temperature for each day.
Pandas
Need a hint?

Use df.resample(freq).mean() to get daily average temperatures.

4
Print daily average temperatures
Print the daily_avg DataFrame to display the daily average temperatures.
Pandas
Need a hint?

Use print(daily_avg) to show the daily average temperatures.