0
0
Data Analysis Pythondata~30 mins

Rolling window calculations in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Rolling Window Calculations
📖 Scenario: You work as a data analyst for a small store. You have daily sales data for a week. You want to understand how sales change over time by calculating the average sales over a rolling window of 3 days.
🎯 Goal: Build a small program that calculates the 3-day rolling average of daily sales using Python and pandas.
📋 What You'll Learn
Create a pandas DataFrame with daily sales data for 7 days.
Set a window size variable for the rolling calculation.
Calculate the rolling average of sales using the window size.
Print the resulting rolling average values.
💡 Why This Matters
🌍 Real World
Rolling window calculations help smooth out short-term fluctuations in data, making it easier to see trends. This is useful in sales, finance, weather data, and many other fields.
💼 Career
Data analysts and scientists often use rolling calculations to prepare data for reports and decision-making, helping businesses understand patterns and make better choices.
Progress0 / 4 steps
1
Create the sales data
Import pandas as pd and create a DataFrame called sales_data with two columns: 'day' containing numbers 1 to 7, and 'sales' containing these exact values: 100, 120, 130, 90, 110, 115, 105.
Data Analysis Python
Hint

Use pd.DataFrame with a dictionary to create the data.

2
Set the rolling window size
Create a variable called window_size and set it to the integer 3 to represent the 3-day window.
Data Analysis Python
Hint

Just assign the number 3 to window_size.

3
Calculate the rolling average
Create a new column in sales_data called 'rolling_avg' that contains the rolling average of the 'sales' column using the window_size. Use the pandas rolling() method followed by mean().
Data Analysis Python
Hint

Use sales_data['sales'].rolling(window=window_size).mean() to get the rolling average.

4
Print the rolling average
Print the 'rolling_avg' column from the sales_data DataFrame to see the rolling averages.
Data Analysis Python
Hint

Use print(sales_data['rolling_avg']) to display the rolling averages.