0
0
Pandasdata~20 mins

Why window functions matter in Pandas - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Function Mastery
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 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())
A[nan, 15.0, 25.0, 35.0, 45.0]
B[10.0, 20.0, 30.0, 40.0, 50.0]
C[nan, nan, 20.0, 30.0, 40.0]
D[nan, nan, nan, 30.0, 40.0]
Attempts:
2 left
💡 Hint
Think about how rolling mean calculates the average of the current and previous values within the window.
data_output
intermediate
2: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)
A
  category  value  cum_sum
0        A      1        1
1        A      2        3
2        B      3        3
3        B      4        7
4        A      5        8
B
  category  value  cum_sum
0        A      1        1
1        A      2        2
2        B      3        3
3        B      4        4
4        A      5        5
C
  category  value  cum_sum
0        A      1        1
1        A      2        3
2        B      3        3
3        B      4        4
4        A      5        5
D
  category  value  cum_sum
0        A      1        1
1        A      2        2
2        B      3        7
3        B      4        7
4        A      5        8
Attempts:
2 left
💡 Hint
Cumulative sum restarts for each group and adds values in order.
visualization
advanced
3: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()
AScatter plot with original data points and rolling average points overlapping exactly
BPie chart showing proportion of original data values
CBar chart showing original data and rolling average side by side for each point
DLine plot with original data as a smooth increasing line and rolling average starting with NaN for first 3 points, then smooth line following original but lagging
Attempts:
2 left
💡 Hint
Rolling average smooths data and has NaN for initial points without enough data.
🧠 Conceptual
advanced
2:00remaining
Why use window functions instead of groupby aggregation?
Which statement best explains why window functions are useful compared to simple groupby aggregation?
AWindow functions allow calculations across rows while keeping the original row structure, unlike groupby which reduces rows to aggregated results.
BWindow functions always run faster than groupby aggregations.
CGroupby aggregation can only be used on numeric data, window functions work on all data types.
DWindow functions replace the need for any filtering or sorting in data processing.
Attempts:
2 left
💡 Hint
Think about whether the number of rows changes after applying each method.
🔧 Debug
expert
2: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())
ASyntaxError due to incorrect use of expanding function
BAll values are NaN because min_periods=5 is greater than the series length
CTypeError because mean() cannot be applied to Series
DIndexError because expanding window exceeds series length
Attempts:
2 left
💡 Hint
Check the length of the series and the min_periods parameter.