0
0
Pandasdata~20 mins

Rolling mean and sum in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rolling Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of rolling mean with window size 3
What is the output of the following code snippet?
Pandas
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.rolling(window=3).mean()
print(result.tolist())
A[nan, nan, 2.0, 3.0, 4.0]
B[1.0, 1.5, 2.0, 3.0, 4.0]
C[nan, nan, 3.0, 4.0, 5.0]
D[nan, 2.0, 3.0, 4.0, 5.0]
Attempts:
2 left
💡 Hint
Remember that rolling mean with window=3 needs 3 values to compute the first mean.
data_output
intermediate
2:00remaining
Sum of rolling window with min_periods
What is the output of the rolling sum with window=3 and min_periods=1 on this series?
Pandas
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.rolling(window=3, min_periods=1).sum()
print(result.tolist())
A[nan, nan, 6.0, 9.0, 12.0]
B[1.0, 3.0, 6.0, 9.0, 12.0]
C[1, 2, 3, 4, 5]
D[1.0, 2.0, 3.0, 4.0, 5.0]
Attempts:
2 left
💡 Hint
min_periods=1 allows calculation even if the window is not full.
🔧 Debug
advanced
2:00remaining
Identify the error in rolling sum code
What error does this code raise?
Pandas
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.rolling(window='3').sum()
print(result.tolist())
AAttributeError: 'str' object has no attribute 'sum'
BValueError: window size must be positive
CTypeError: window must be an integer
DNo error, outputs rolling sum
Attempts:
2 left
💡 Hint
Check the type of the window parameter.
visualization
advanced
3:00remaining
Plot rolling mean and sum
Which option correctly plots both rolling mean and rolling sum with window=2 for the series?
Pandas
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series([1, 3, 5, 7, 9])
rm = s.rolling(window=2).mean()
rs = s.rolling(window=2).sum()
plt.plot(s, label='Original')
plt.plot(rm, label='Rolling Mean')
plt.plot(rs, label='Rolling Sum')
plt.legend()
plt.show()
APlots three lines: original, rolling mean (smooth), rolling sum (higher values)
BPlots only original and rolling mean, rolling sum missing
CPlots original and rolling sum, rolling mean missing
DPlots original twice, no rolling calculations
Attempts:
2 left
💡 Hint
Check which lines are plotted with plt.plot calls.
🚀 Application
expert
3:00remaining
Calculate rolling weighted mean with custom weights
Given the series s = pd.Series([2, 4, 6, 8, 10]) and weights [0.1, 0.3, 0.6], which code correctly calculates the rolling weighted mean with window=3?
As.rolling(window=3).apply(lambda x: (x * [0.1, 0.3, 0.6]).mean())
Bs.rolling(window=3).apply(lambda x: (x * [0.6, 0.3, 0.1]).sum())
Cs.rolling(window=3).apply(lambda x: (x * [0.1, 0.3]).sum())
Ds.rolling(window=3).apply(lambda x: (x * [0.1, 0.3, 0.6]).sum())
Attempts:
2 left
💡 Hint
Weights should align with the order of values in the window from oldest to newest.