0
0
Pandasdata~15 mins

rolling() for moving windows in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Using pandas rolling() for Moving Window Calculations
📖 Scenario: You work as a data analyst for a small bakery. You have daily sales data for a week. You want to understand the trend by calculating the moving average of sales over a 3-day window.
🎯 Goal: Build a small program that uses pandas rolling() to calculate the 3-day moving average of daily sales and display the result.
📋 What You'll Learn
Create a pandas DataFrame with exact daily sales data for 7 days
Create a variable for the rolling window size set to 3
Use the rolling() method on the sales column with the window size variable
Calculate the mean for each rolling window
Print the resulting moving average series
💡 Why This Matters
🌍 Real World
Moving averages help smooth out short-term fluctuations in data, making it easier to see trends. Businesses use this to analyze sales, stock prices, or website traffic.
💼 Career
Data analysts and scientists often use rolling window calculations to prepare data for reports and forecasting.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales_data with a column 'sales' containing these exact values: [10, 20, 15, 25, 30, 20, 40].
Pandas
Need a hint?

Use pd.DataFrame and pass a dictionary with key 'sales' and the list of values.

2
Set the rolling window size
Create a variable called window_size and set it to 3.
Pandas
Need a hint?

Just assign the number 3 to the variable window_size.

3
Calculate the 3-day moving average
Create a new variable called moving_avg that uses sales_data['sales'].rolling(window_size).mean() to calculate the moving average.
Pandas
Need a hint?

Use rolling(window_size) on the sales column, then call mean().

4
Print the moving average result
Print the moving_avg variable to display the 3-day moving average values.
Pandas
Need a hint?

Use print(moving_avg) to show the result.