Bird
Raised Fist0
Pandasdata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the rolling(window=3).mean() function do on a pandas Series?
easy
A. Calculates the average of the current and previous 2 values for each position.
B. Calculates the sum of all values in the Series.
C. Calculates the difference between consecutive values.
D. Sorts the Series in ascending order.

Solution

  1. Step 1: Understand rolling window size

    The window=3 means we look at 3 values at a time: the current and previous 2.
  2. Step 2: Apply mean function on each window

    For each position, the mean is calculated over these 3 values, smoothing the data.
  3. Final Answer:

    Calculates the average of the current and previous 2 values for each position. -> Option A
  4. Quick Check:

    Rolling mean = average over window [OK]
Hint: Rolling mean averages over the window size including current [OK]
Common Mistakes:
  • Thinking it sums all values, not just window
  • Confusing mean with difference
  • Assuming it sorts data
2. Which of the following is the correct syntax to calculate a rolling sum with a window size of 4 on a pandas DataFrame column named 'sales'?
easy
A. rolling(df['sales'], window=4).sum()
B. df.rolling('sales', 4).sum()
C. df['sales'].rolling(4).sum()
D. df['sales'].sum().rolling(4)

Solution

  1. Step 1: Identify correct method chaining

    In pandas, rolling is called on the Series, then sum() is applied.
  2. Step 2: Check syntax correctness

    df['sales'].rolling(4).sum() correctly uses df['sales'].rolling(4).sum() which is valid syntax.
  3. Final Answer:

    df['sales'].rolling(4).sum() -> Option C
  4. Quick Check:

    Correct rolling sum syntax = df['sales'].rolling(4).sum() [OK]
Hint: Use Series.rolling(window).sum() for rolling sums [OK]
Common Mistakes:
  • Passing column name as string to rolling()
  • Calling sum() before rolling()
  • Using undefined rolling() function
3. Given the pandas Series data = pd.Series([2, 4, 6, 8, 10]), what is the output of data.rolling(window=3).sum()?
medium
A. [2, 6, 12, 18, 24]
B. [NaN, 6, 12, 18, 24]
C. [2, 4, 6, 8, 10]
D. [NaN, NaN, 12, 18, 24]

Solution

  1. Step 1: Understand rolling sum with window=3

    For the first two positions, there are not enough values to fill the window, so result is NaN.
  2. Step 2: Calculate sums for positions 3 to 5

    Position 3 sum: 2+4+6=12, position 4 sum: 4+6+8=18, position 5 sum: 6+8+10=24.
  3. Final Answer:

    [NaN, NaN, 12, 18, 24] -> Option D
  4. Quick Check:

    Rolling sum with window=3 pads start with NaN [OK]
Hint: Rolling sum outputs NaN until window fills [OK]
Common Mistakes:
  • Expecting sums from first element
  • Confusing rolling sum with cumulative sum
  • Ignoring NaN for incomplete windows
4. What is wrong with this code snippet to calculate rolling mean on column 'temp' with window 2?
df = pd.DataFrame({'temp': [10, 20, 30]})
df['rolling_mean'] = df.rolling(2).mean()['temp']
medium
A. The code is correct and will run without errors.
B. The window size must be 3 or more.
C. mean() cannot be used after rolling().
D. The rolling() should be called on the column, not the whole DataFrame.

Solution

  1. Step 1: Analyze the code execution

    df.rolling(2).mean() applies the rolling mean to the 'temp' column, producing a DataFrame with values [NaN, 15.0, 25.0].
  2. Step 2: Column access and assignment

    df.rolling(2).mean()['temp'] extracts the Series correctly, and assignment to df['rolling_mean'] succeeds without errors.
  3. Final Answer:

    The code is correct and will run without errors. -> Option A
  4. Quick Check:

    df.rolling(2).mean()['temp'] works for column rolling mean [OK]
Hint: df.rolling(window).mean()['col'] is valid for single-column rolling [OK]
Common Mistakes:
  • Believing rolling() must only be called on Series
  • Thinking window size must be 3 or more
  • Assuming mean() cannot be used after rolling()
5. You have daily sales data in a pandas DataFrame df with column 'sales'. You want to create a new column 'weekly_avg' that shows the average sales over the last 7 days, but only if at least 5 days of data are available in the window. Which code correctly achieves this?
hard
A. df['weekly_avg'] = df['sales'].rolling(7).mean(min_periods=5)
B. df['weekly_avg'] = df['sales'].rolling(window=7, min_periods=5).mean()
C. df['weekly_avg'] = df['sales'].rolling(window=5).mean()
D. df['weekly_avg'] = df['sales'].rolling(window=7).sum() / 5

Solution

  1. Step 1: Understand rolling with min_periods

    min_periods=5 means the rolling function will compute mean only if at least 5 values exist in the window.
  2. Step 2: Check correct syntax for rolling mean with conditions

    df['weekly_avg'] = df['sales'].rolling(window=7, min_periods=5).mean() correctly uses rolling(window=7, min_periods=5).mean() to get average over 7 days with minimum 5 days data.
  3. Final Answer:

    df['weekly_avg'] = df['sales'].rolling(window=7, min_periods=5).mean() -> Option B
  4. Quick Check:

    Use min_periods to require minimum data in rolling [OK]
Hint: Use min_periods in rolling() to set minimum data points [OK]
Common Mistakes:
  • Passing min_periods to mean() instead of rolling()
  • Using wrong window size
  • Dividing sum by fixed number ignoring data count
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard