0
0
Pandasdata~30 mins

Rolling mean and sum in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Rolling mean and sum
📖 Scenario: You work as a data analyst for a small store. You have daily sales data for a week. Your manager wants to see the average and total sales over the last 3 days for each day to understand trends better.
🎯 Goal: Build a program that calculates the rolling mean and rolling sum of sales over a 3-day window using pandas.
📋 What You'll Learn
Create a pandas DataFrame with exact sales data for 7 days.
Set a window size variable for rolling calculations.
Calculate rolling mean and rolling sum using pandas.
Print the resulting DataFrame showing original sales, rolling mean, and rolling sum.
💡 Why This Matters
🌍 Real World
Rolling averages and sums help smooth out daily fluctuations in sales data, making it easier to spot trends and patterns over time.
💼 Career
Data analysts and scientists often use rolling calculations to analyze time series data in finance, sales, weather, and many other fields.
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, 30, 25, 40, 35].
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 to represent the 3-day rolling window.
Pandas
Need a hint?

Just assign the number 3 to the variable window_size.

3
Calculate rolling mean and sum
Use the rolling method on sales_data['sales'] with the window size window_size to create two new columns: 'rolling_mean' and 'rolling_sum' that store the rolling mean and rolling sum respectively.
Pandas
Need a hint?

Use .rolling(window=window_size).mean() and .rolling(window=window_size).sum() on the sales column.

4
Print the final DataFrame
Print the sales_data DataFrame to display the original sales, rolling mean, and rolling sum columns.
Pandas
Need a hint?

Use print(sales_data) to show the DataFrame.