Complete the code to shift the 'sales' column down by one row.
df['previous_sales'] = df['sales'].[1](1)
The shift function moves the data down by the specified number of rows, creating a lag effect.
Complete the code to shift the 'temperature' column up by two rows.
df['future_temp'] = df['temperature'].[1](-2)
Using shift(-2) moves the data up by two rows, showing future values.
Fix the error in the code to correctly create a lagged column named 'lagged_price'.
df['lagged_price'] = df.price.[1](1)
The correct pandas method to create lagged columns is shift. Using df.price.shift(1) works correctly.
Fill both blanks to create a 3-period lag for the 'sales' column.
df['sales_lag3'] = df['sales'].[1]([2])
To lag by 3 periods, use shift(3). Positive periods shift index down, equivalent to lagging the values.
Fill all three blanks to compute the next sales per store group, filling missing with 0.
df['next_sales'] = df.groupby('store')['sales'].[1]([2]).[3](0)
groupby('store')['sales'].shift(-1).fillna(0) creates group-wise leads (next values) and fills trailing NaNs with 0.