0
0
Pandasdata~10 mins

Rolling mean and sum 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 of the 'sales' column with a window size of 3.

Pandas
df['rolling_mean'] = df['sales'].[1](3).mean()
Drag options to blanks, or click blank then click option'
Acumsum
Bgroupby
Crolling
Dsort_values
Attempts:
3 left
💡 Hint
Common Mistakes
Using groupby instead of rolling
Using cumsum which calculates cumulative sum, not rolling mean
2fill in blank
medium

Complete the code to calculate the rolling sum of the 'temperature' column with a window size of 5.

Pandas
df['rolling_sum'] = df['temperature'].[1](5).sum()
Drag options to blanks, or click blank then click option'
Acumsum
Bgroupby
Cexpanding
Drolling
Attempts:
3 left
💡 Hint
Common Mistakes
Using cumsum which sums all previous values cumulatively
Using expanding which grows the window size instead of fixed size
3fill in blank
hard

Fix the error in the code to correctly calculate the rolling mean with a window size of 4.

Pandas
rolling_avg = df['value'].rolling([1]).mean()
Drag options to blanks, or click blank then click option'
A4
B'4'
Cwindow=4
Dsize=4
Attempts:
3 left
💡 Hint
Common Mistakes
Passing window size as a string
Using incorrect keyword arguments like window=4
4fill in blank
hard

Fill both blanks to create a rolling sum with window size 3 and minimum periods 2.

Pandas
rolling_sum = df['data'].[1](3, [2]=2).sum()
Drag options to blanks, or click blank then click option'
Arolling
Bmin_periods
Cwindow
Dcumsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using cumsum instead of rolling
Using window instead of min_periods for minimum data points
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each word to its rolling mean length if length is greater than 3.

Pandas
result = {word: df['lengths'].rolling([1]).mean().iloc[i] for i, word in enumerate(words) if [2](word) [3] 3}
Drag options to blanks, or click blank then click option'
A3
Blen
C>
Drolling
Attempts:
3 left
💡 Hint
Common Mistakes
Using rolling instead of window size number
Using incorrect comparison operators
Not using len() to get word length