0
0
Pandasdata~10 mins

ewm() for exponential moving average 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 exponential moving average of the 'price' column with a span of 3.

Pandas
df['ema'] = df['price'].[1](span=3).mean()
Drag options to blanks, or click blank then click option'
Aewm
Brolling
Cexpanding
Dcumsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using rolling() instead of ewm() which calculates simple moving average.
Forgetting to call mean() after ewm().
2fill in blank
medium

Complete the code to calculate the exponential moving average with a halflife of 2.

Pandas
df['ema'] = df['price'].ewm([1]=2).mean()
Drag options to blanks, or click blank then click option'
Acom
Bspan
Calpha
Dhalflife
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'span' instead of 'halflife' when the question asks specifically for half-life.
Confusing 'alpha' with halflife.
3fill in blank
hard

Fix the error in the code to correctly calculate the EMA with center of mass 1.5.

Pandas
df['ema'] = df['price'].ewm(com=[1]).mean()
Drag options to blanks, or click blank then click option'
A1.5
B'1.5'
Ccom=1.5
D1,5
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the value as a string with quotes.
Including the parameter name inside the parentheses again.
Using comma instead of dot for decimal.
4fill in blank
hard

Fill both blanks to create an EMA with alpha 0.3 and ignore NaN values in the calculation.

Pandas
df['ema'] = df['price'].ewm([1]=0.3, [2]=True).mean()
Drag options to blanks, or click blank then click option'
Aalpha
Badjust
Cignore_na
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'adjust' instead of 'ignore_na'.
Confusing 'adjust' with 'ignore_na'.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that calculates EMA for each column in df with span 4 and stores results in a new dict.

Pandas
ema_dict = [1]: df[col].ewm([2]=4).mean() for col in df.columns if col [3] 'price'
Drag options to blanks, or click blank then click option'
Acol
Bspan
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' to exclude the 'price' column.
Using a wrong key in the dictionary comprehension.
Using 'com' instead of 'span' for the parameter.