0
0
Pandasdata~30 mins

Expanding window operations in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Expanding Window Operations with pandas
📖 Scenario: You work as a data analyst for a small online store. You have daily sales data and want to understand how the total sales grow over time. Instead of just looking at daily sales, you want to see the running total of sales up to each day.
🎯 Goal: Build a pandas DataFrame with daily sales data, then use expanding window operations to calculate the running total of sales for each day.
📋 What You'll Learn
Create a pandas DataFrame with exact daily sales data
Create a variable to hold the sales column name
Use pandas expanding() method to calculate running total sales
Print the resulting DataFrame with the running total column
💡 Why This Matters
🌍 Real World
Expanding window operations help analyze cumulative trends in sales, stock prices, or any time series data.
💼 Career
Data analysts and scientists use expanding windows to summarize data over time and detect patterns or growth.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales_data with two columns: 'day' and 'sales'. Use these exact values: days from 1 to 5, and sales as 100, 150, 200, 130, 170.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary containing the exact lists for 'day' and 'sales'.

2
Set the sales column name
Create a variable called sales_column and set it to the string 'sales'.
Pandas
Need a hint?

Just assign the string 'sales' to the variable sales_column.

3
Calculate running total sales using expanding window
Create a new column in sales_data called 'running_total' by applying the pandas expanding() method on the sales_column and then calling sum().
Pandas
Need a hint?

Use sales_data[sales_column].expanding().sum() to get the running total and assign it to a new column.

4
Print the DataFrame with running totals
Write a print() statement to display the sales_data DataFrame with the new 'running_total' column.
Pandas
Need a hint?

Use print(sales_data) to show the DataFrame with the running totals.