0
0
Pandasdata~10 mins

pct_change() for percentage change 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 percentage change of the 'sales' column in the DataFrame.

Pandas
df['pct_change'] = df['sales'].[1]()
Drag options to blanks, or click blank then click option'
Adiff
Bmean
Ccumsum
Dpct_change
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'diff()' which calculates difference, not percentage change.
Using 'cumsum()' which calculates cumulative sum.
Using 'mean()' which calculates average.
2fill in blank
medium

Complete the code to calculate the percentage change with a period of 2 rows.

Pandas
df['pct_change_2'] = df['revenue'].[1](periods=2)
Drag options to blanks, or click blank then click option'
Apct_change
Bdiff
Cshift
Drolling
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'diff()' which does not accept 'periods' for percentage change.
Using 'shift()' which shifts data but does not calculate percentage change.
Using 'rolling()' which is for moving window calculations.
3fill in blank
hard

Fix the error in the code to correctly calculate percentage change of the 'price' column.

Pandas
df['price_change'] = df['price'].[1](periods=1)
Drag options to blanks, or click blank then click option'
Apctchange
Bpct_change
Cpct_changes
Dpercent_change
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the function name.
Using 'period' instead of 'periods' parameter.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

Pandas
{word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Checking if the word is greater than 3 instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each uppercase word to its value only if the value is greater than 0.

Pandas
{ [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original key without uppercase.
Using incorrect comparison operators.
Swapping keys and values.