Window Functions are advanced data analysis techniques in Pandas that perform calculations over a group of rows instead of individual values. They are commonly used for moving averages, cumulative totals, running balances, trend analysis, financial forecasting, stock market analysis, and time-series analytics.
Pandas provides powerful window functions such as rolling(), expanding(), cumsum(), cumprod(), cummax(), and cummin(). These functions are widely used in business intelligence dashboards, sales reporting, finance, inventory management, and machine learning preprocessing. Pandas Window Functions practice questions with solutions help to understand the concepts.
In this chapter, you’ll solve practical questions based on rolling windows, expanding windows, and cumulative calculations.
1. Python Program to Calculate Cumulative Sum
Problem Statement
Write a Python program to calculate the cumulative sum of monthly sales.
Python Solution
import pandas as pd
data = {
"Sales": [1000, 1500, 1200, 1800, 2000]
}
df = pd.DataFrame(data)
df["Cumulative Sales"] = df["Sales"].cumsum()
print(df)
Sample Output
Sales Cumulative Sales
0 1000 1000
1 1500 2500
2 1200 3700
3 1800 5500
4 2000 7500
Explanation
The cumsum() function calculates a running total by continuously adding each value to the previous sum.
Concepts Covered
cumsum()- Running Total
- Cumulative Calculation
2. Python Program to Calculate Cumulative Product
Problem Statement
Write a Python program to calculate the cumulative product of values.
Python Solution
import pandas as pd
data = {
"Value": [2, 3, 4, 5]
}
df = pd.DataFrame(data)
df["Cumulative Product"] = df["Value"].cumprod()
print(df)
Sample Output
Value Cumulative Product
0 2 2
1 3 6
2 4 24
3 5 120
Explanation
The cumprod() function multiplies each value with the cumulative result of previous values.
Concepts Covered
cumprod()- Running Product
- Window Functions
3. Python Program to Find the Cumulative Maximum
Problem Statement
Write a Python program to calculate the cumulative maximum value.
Python Solution
import pandas as pd
data = {
"Sales": [1200, 1800, 1500, 2500, 2200]
}
df = pd.DataFrame(data)
df["Running Maximum"] = df["Sales"].cummax()
print(df)
Sample Output
Sales Running Maximum
0 1200 1200
1 1800 1800
2 1500 1800
3 2500 2500
4 2200 2500
Explanation
The cummax() function continuously tracks the highest value encountered so far.
Concepts Covered
cummax()- Running Maximum
- Trend Analysis
4. Python Program to Find the Cumulative Minimum
Problem Statement
Write a Python program to calculate the cumulative minimum value.
Python Solution
import pandas as pd
data = {
"Price": [80, 70, 75, 65, 90]
}
df = pd.DataFrame(data)
df["Running Minimum"] = df["Price"].cummin()
print(df)
Sample Output
Price Running Minimum
0 80 80
1 70 70
2 75 70
3 65 65
4 90 65
Explanation
The cummin() function keeps track of the smallest value seen up to the current row.
Concepts Covered
cummin()- Running Minimum
- Data Analysis
5. Python Program to Calculate a 3-Day Rolling Average
Problem Statement
Write a Python program to calculate a rolling average using a window size of 3.
Python Solution
import pandas as pd
data = {
"Sales": [1000, 1200, 1500, 1800, 2000]
}
df = pd.DataFrame(data)
df["Rolling Average"] = (
df["Sales"]
.rolling(window=3)
.mean()
)
print(df)
Sample Output
Sales Rolling Average
0 1000 NaN
1 1200 NaN
2 1500 1233.333333
3 1800 1500.000000
4 2000 1766.666667
Explanation
The rolling(window=3) function creates a moving window of three rows, and mean() calculates the average for each window.
Concepts Covered
rolling()- Moving Average
- Window Size
6. Python Program to Calculate a 3-Day Rolling Sum
Problem Statement
Write a Python program to calculate a rolling sum using a window size of 3.
Python Solution
import pandas as pd
data = {
"Sales": [1000, 1200, 1500, 1800, 2000]
}
df = pd.DataFrame(data)
df["Rolling Sum"] = (
df["Sales"]
.rolling(window=3)
.sum()
)
print(df)
Sample Output
Sales Rolling Sum
0 1000 NaN
1 1200 NaN
2 1500 3700.0
3 1800 4500.0
4 2000 5300.0
Explanation
The rolling(window=3).sum() function calculates the total of every three consecutive rows.
Concepts Covered
rolling()sum()- Moving Total
7. Python Program to Calculate a Rolling Maximum
Problem Statement
Write a Python program to find the highest value within a rolling window of 3 rows.
Python Solution
import pandas as pd
data = {
"Sales": [1000, 1200, 1500, 1800, 1700]
}
df = pd.DataFrame(data)
df["Rolling Maximum"] = (
df["Sales"]
.rolling(window=3)
.max()
)
print(df)
Sample Output
Sales Rolling Maximum
0 1000 NaN
1 1200 NaN
2 1500 1500.0
3 1800 1800.0
4 1700 1800.0
Explanation
The rolling().max() function returns the highest value in each rolling window.
Concepts Covered
rolling()max()- Moving Maximum
8. Python Program to Calculate a Rolling Minimum
Problem Statement
Write a Python program to find the smallest value within a rolling window of 3 rows.
Python Solution
import pandas as pd
data = {
"Price": [80, 70, 90, 60, 85]
}
df = pd.DataFrame(data)
df["Rolling Minimum"] = (
df["Price"]
.rolling(window=3)
.min()
)
print(df)
Sample Output
Price Rolling Minimum
0 80 NaN
1 70 NaN
2 90 70.0
3 60 60.0
4 85 60.0
Explanation
The rolling().min() function returns the smallest value within each moving window.
Concepts Covered
rolling()min()- Moving Minimum
9. Python Program to Calculate an Expanding Sum
Problem Statement
Write a Python program to calculate an expanding sum.
Python Solution
import pandas as pd
data = {
"Sales": [1000, 1500, 1200, 1800]
}
df = pd.DataFrame(data)
df["Expanding Sum"] = (
df["Sales"]
.expanding()
.sum()
)
print(df)
Sample Output
Sales Expanding Sum
0 1000 1000.0
1 1500 2500.0
2 1200 3700.0
3 1800 5500.0
Explanation
The expanding() function includes all rows from the beginning up to the current row.
Concepts Covered
expanding()- Running Total
- Expanding Window
10. Python Program to Calculate an Expanding Average
Problem Statement
Write a Python program to calculate the expanding average.
Python Solution
import pandas as pd
data = {
"Marks": [70, 80, 90, 100]
}
df = pd.DataFrame(data)
df["Expanding Average"] = (
df["Marks"]
.expanding()
.mean()
)
print(df)
Sample Output
Marks Expanding Average
0 70 70.0
1 80 75.0
2 90 80.0
3 100 85.0
Explanation
The expanding().mean() function calculates the average from the first row up to the current row.
Concepts Covered
expanding()mean()- Running Average
11. Python Program to Calculate a 2-Day Rolling Standard Deviation
Problem Statement
Write a Python program to calculate the rolling standard deviation using a window size of 2.
Python Solution
import pandas as pd
data = {
"Sales": [1000, 1200, 1500, 1800, 2000]
}
df = pd.DataFrame(data)
df["Rolling Std"] = (
df["Sales"]
.rolling(window=2)
.std()
)
print(df)
Sample Output
Sales Rolling Std
0 1000 NaN
1 1200 141.421356
2 1500 212.132034
3 1800 212.132034
4 2000 141.421356
Explanation
The rolling().std() function calculates the standard deviation for each rolling window.
Concepts Covered
rolling()std()- Rolling Standard Deviation
12. Python Program to Calculate a 3-Day Rolling Variance
Problem Statement
Write a Python program to calculate the rolling variance using a window size of 3.
Python Solution
import pandas as pd
data = {
"Sales": [1000, 1200, 1500, 1800, 2000]
}
df = pd.DataFrame(data)
df["Rolling Variance"] = (
df["Sales"]
.rolling(window=3)
.var()
)
print(df)
Sample Output
Sales Rolling Variance
0 1000 NaN
1 1200 NaN
2 1500 63333.333333
3 1800 90000.000000
4 2000 63333.333333
Explanation
The rolling().var() function calculates the variance within each rolling window.
Concepts Covered
rolling()var()- Rolling Variance
13. Python Program to Calculate an Expanding Maximum
Problem Statement
Write a Python program to calculate the expanding maximum value.
Python Solution
import pandas as pd
data = {
"Sales": [1200, 1500, 1300, 1800, 1700]
}
df = pd.DataFrame(data)
df["Expanding Maximum"] = (
df["Sales"]
.expanding()
.max()
)
print(df)
Sample Output
Sales Expanding Maximum
0 1200 1200.0
1 1500 1500.0
2 1300 1500.0
3 1800 1800.0
4 1700 1800.0
Explanation
The expanding().max() function continuously tracks the highest value from the beginning of the dataset.
Concepts Covered
expanding()max()- Running Maximum
14. Python Program to Calculate an Expanding Minimum
Problem Statement
Write a Python program to calculate the expanding minimum value.
Python Solution
import pandas as pd
data = {
"Price": [80, 70, 90, 65, 85]
}
df = pd.DataFrame(data)
df["Expanding Minimum"] = (
df["Price"]
.expanding()
.min()
)
print(df)
Sample Output
Price Expanding Minimum
0 80 80.0
1 70 70.0
2 90 70.0
3 65 65.0
4 85 65.0
Explanation
The expanding().min() function continuously tracks the smallest value encountered.
Concepts Covered
expanding()min()- Running Minimum
15. Python Program to Calculate a Rolling Median
Problem Statement
Write a Python program to calculate the rolling median using a window size of 3.
Python Solution
import pandas as pd
data = {
"Marks": [60, 75, 90, 80, 95]
}
df = pd.DataFrame(data)
df["Rolling Median"] = (
df["Marks"]
.rolling(window=3)
.median()
)
print(df)
Sample Output
Marks Rolling Median
0 60 NaN
1 75 NaN
2 90 75.0
3 80 80.0
4 95 90.0
Explanation
The rolling().median() function calculates the median value for each rolling window.
Concepts Covered
rolling()median()- Moving Median
Chapter Summary
In this chapter, you learned how to use Pandas Window Functions for advanced data analysis. You practiced cumulative functions such as cumsum(), cumprod(), cummax(), and cummin(), along with rolling calculations like moving average, moving sum, rolling maximum, minimum, variance, standard deviation, and median. You also explored expanding window calculations for cumulative averages, sums, maximums, and minimums. These techniques are widely used in financial analysis, sales forecasting, time-series analytics, KPI reporting, and business intelligence dashboards.
Key Takeaways
cumsum()calculates a running total.cumprod()calculates a running product.cummax()tracks the highest value encountered.cummin()tracks the lowest value encountered.rolling(window=n)performs calculations over a moving window.expanding()performs calculations from the first row to the current row.- Rolling functions include
mean(),sum(),min(),max(),median(),std(), andvar(). - Window functions are commonly used in trend analysis, forecasting, and financial reporting.
- Rolling calculations depend on the specified window size.
- Expanding calculations always include all previous rows.
Frequently Asked Questions (FAQs)
1. What are Window Functions in Pandas?
Window functions perform calculations over a group of rows instead of a single row. They are commonly used for moving averages, cumulative totals, and trend analysis.
2. How do you calculate a cumulative sum?
df["Sales"].cumsum()
3. How do you calculate a rolling average?
df["Sales"].rolling(window=3).mean()
4. How do you calculate a rolling sum?
df["Sales"].rolling(window=3).sum()
5. What is the difference between rolling() and expanding()?
rolling()performs calculations over a fixed-size moving window.expanding()performs calculations using all rows from the beginning up to the current row.
6. How do you calculate a cumulative maximum?
df["Sales"].cummax()
7. How do you calculate a rolling standard deviation?
df["Sales"].rolling(window=2).std()
8. Why are Window Functions important in Pandas?
Window functions are essential for analyzing trends, forecasting sales, calculating running totals, smoothing time-series data, generating KPIs, monitoring financial performance, and building advanced business intelligence reports.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.