0
0
Pandasdata~10 mins

rolling() for moving windows in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the rolling mean with a window size of 3.

Pandas
import pandas as pd

data = pd.Series([1, 2, 3, 4, 5])
rolling_mean = data.[1](3).mean()
print(rolling_mean.tolist())
Drag options to blanks, or click blank then click option'
Acumsum
Brolling
Csort_values
Dgroupby
Attempts:
3 left
💡 Hint
Common Mistakes
Using groupby instead of rolling causes an error.
Using cumsum calculates cumulative sum, not rolling mean.
2fill in blank
medium

Complete the code to calculate the rolling sum with a window size of 4.

Pandas
import pandas as pd

values = pd.Series([10, 20, 30, 40, 50])
rolling_sum = values.[1](4).sum()
print(rolling_sum.tolist())
Drag options to blanks, or click blank then click option'
Arolling
Bexpanding
Ccumsum
Ddiff
Attempts:
3 left
💡 Hint
Common Mistakes
Using cumsum sums all values up to current, not in a fixed window.
Using diff calculates differences, not sums.
3fill in blank
hard

Fix the error in the code to calculate the rolling max with a window size of 2.

Pandas
import pandas as pd

scores = pd.Series([5, 3, 8, 7])
rolling_max = scores.[1](window=2).max()
print(rolling_max.tolist())
Drag options to blanks, or click blank then click option'
Arolling
Brolling_window
Cwindow_rolling
Drollingmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent functions like rolling_window or rollingmean.
Misspelling the function name.
4fill in blank
hard

Fill both blanks to create a rolling window of size 3 and calculate the rolling standard deviation.

Pandas
import pandas as pd

values = pd.Series([2, 4, 6, 8, 10])
rolling_std = values.[1]([2]).std()
print(rolling_std.tolist())
Drag options to blanks, or click blank then click option'
Arolling
B3
C5
Dwindow
Attempts:
3 left
💡 Hint
Common Mistakes
Using window=5 instead of 3 changes the window size.
Using 'window' as a parameter instead of a number.
5fill in blank
hard

Fill all three blanks to create a rolling window of size 2, calculate the rolling median, and assign it to a new column in a DataFrame.

Pandas
import pandas as pd

data = pd.DataFrame({'values': [1, 3, 5, 7, 9]})
data['rolling_median'] = data['values'].[1]([2]).[3]()
print(data)
Drag options to blanks, or click blank then click option'
Arolling
B2
Cmedian
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean() instead of median() changes the result.
Forgetting to assign the result to a new column.