0
0
Data Analysis Pythondata~30 mins

Resampling time series in Data Analysis Python - 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, but your manager wants to see the average temperature for each day instead. To do this, you need to resample the hourly data into daily data.
🎯 Goal: Learn how to resample time series data from hourly to daily frequency and calculate the daily average temperature.
📋 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 temperature
Print the resulting daily average temperature DataFrame
💡 Why This Matters
🌍 Real World
Weather stations and many other fields collect data at high frequency but often need summaries at lower frequency for reports or analysis.
💼 Career
Data analysts and scientists frequently resample time series data to extract meaningful insights and prepare data for visualization or modeling.
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-02 23:00' at hourly intervals. Add a column called 'temperature' with these exact values: [30, 31, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16].
Data Analysis Python
Hint

Use pd.date_range to create the datetime index and pass it as the index when creating the DataFrame.

2
Set the resampling frequency
Create a variable called freq and set it to the string 'D' to represent daily frequency for resampling.
Data Analysis Python
Hint

Set freq to the string 'D' to represent daily frequency.

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.
Data Analysis Python
Hint

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

4
Print the daily average temperature
Print the daily_avg DataFrame to display the average temperature for each day.
Data Analysis Python
Hint

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