Challenge - 5 Problems
Window Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of rolling mean with window size 3
What is the output of the following code that calculates a rolling mean with a window size of 3 on a pandas Series?
Pandas
import pandas as pd s = pd.Series([10, 20, 30, 40, 50]) result = s.rolling(window=3).mean() print(result.tolist())
Attempts:
2 left
💡 Hint
Think about how rolling mean calculates the average of the current and previous values within the window.
✗ Incorrect
The rolling mean with window=3 calculates the average of the current and previous two values. The first two positions have insufficient data, so they are NaN. Starting from the third value, the mean is computed over the last three values.
❓ data_output
intermediate2:00remaining
Result of cumulative sum grouped by category
Given the DataFrame below, what is the output of the cumulative sum of 'value' grouped by 'category'?
Pandas
import pandas as pd df = pd.DataFrame({'category': ['A', 'A', 'B', 'B', 'A'], 'value': [1, 2, 3, 4, 5]}) df['cum_sum'] = df.groupby('category')['value'].cumsum() print(df)
Attempts:
2 left
💡 Hint
Cumulative sum restarts for each group and adds values in order.
✗ Incorrect
The cumulative sum adds values within each category group. For category A: 1, then 1+2=3, then 3+5=8. For category B: 3, then 3+4=7.
❓ visualization
advanced3:00remaining
Visualizing rolling average impact
Which plot correctly shows the original data and its rolling average with window size 4?
Pandas
import pandas as pd import matplotlib.pyplot as plt s = pd.Series([5, 10, 15, 20, 25, 30, 35]) rolling_avg = s.rolling(window=4).mean() plt.plot(s, label='Original') plt.plot(rolling_avg, label='Rolling Avg') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Rolling average smooths data and has NaN for initial points without enough data.
✗ Incorrect
The rolling average line starts with NaN for the first 3 points (window=4), then follows the trend of original data but smoother and slightly lagged. A line plot best shows this.
🧠 Conceptual
advanced2:00remaining
Why use window functions instead of groupby aggregation?
Which statement best explains why window functions are useful compared to simple groupby aggregation?
Attempts:
2 left
💡 Hint
Think about whether the number of rows changes after applying each method.
✗ Incorrect
Window functions compute values across rows but keep the original number of rows, allowing more detailed analysis. Groupby aggregation reduces rows by summarizing groups.
🔧 Debug
expert2:30remaining
Identify the error in this expanding window code
What error does the following code produce and why?
import pandas as pd
s = pd.Series([2, 4, 6, 8])
result = s.expanding(min_periods=5).mean()
print(result.tolist())
Pandas
import pandas as pd s = pd.Series([2, 4, 6, 8]) result = s.expanding(min_periods=5).mean() print(result.tolist())
Attempts:
2 left
💡 Hint
Check the length of the series and the min_periods parameter.
✗ Incorrect
The expanding window requires at least 5 data points to compute the mean, but the series has only 4 points, so all results are NaN.