0
0
Data Analysis Pythondata~10 mins

Shift and lag operations in Data Analysis Python - Interactive Code Practice

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

Complete the code to shift the 'sales' column down by one row.

Data Analysis Python
df['previous_sales'] = df['sales'].[1](1)
Drag options to blanks, or click blank then click option'
Aroll
Blag
Cshift
Dmove
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lag' which is not a pandas function.
Trying to use 'roll' or 'move' which do not exist in pandas.
2fill in blank
medium

Complete the code to shift the 'temperature' column up by two rows.

Data Analysis Python
df['future_temp'] = df['temperature'].[1](-2)
Drag options to blanks, or click blank then click option'
Aroll
Blag
Cmove
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'roll' or 'move' which are not pandas functions.
Using positive numbers to shift up.
3fill in blank
hard

Fix the error in the code to correctly create a lagged column named 'lagged_price'.

Data Analysis Python
df['lagged_price'] = df.price.[1](1)
Drag options to blanks, or click blank then click option'
Ashift
Blag
Cmove
Droll
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lag' which is not a pandas method.
Using dot notation without quotes for column names is okay but method must be correct.
4fill in blank
hard

Fill both blanks to create a 3-period lag for the 'sales' column.

Data Analysis Python
df['sales_lag3'] = df['sales'].[1]([2])
Drag options to blanks, or click blank then click option'
Ashift
B3
Clag
Ddiff
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lag' which is not a pandas method.
Using negative number which would create a lead instead of lag.
5fill in blank
hard

Fill all three blanks to compute the next sales per store group, filling missing with 0.

Data Analysis Python
df['next_sales'] = df.groupby('store')['sales'].[1]([2]).[3](0)
Drag options to blanks, or click blank then click option'
Ashift
B-1
Cfillna
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting groupby, causing global shift instead of per-group.
Using positive periods for next values (would be lag).
Forgetting fillna to replace NaNs.