Complete the code to calculate the difference between consecutive values in the 'sales' column.
df['diff_sales'] = df['sales'].[1]()
The diff() function calculates the difference between consecutive rows in a column.
Complete the code to calculate the difference over 2 periods in the 'temperature' column.
df['temp_diff_2'] = df['temperature'].diff([1])
Passing 2 to diff() calculates the difference between the current row and the row 2 steps before.
Fix the error in the code to correctly calculate the difference of the 'price' column.
df['price_diff'] = df.price.[1]()
The correct method name is diff(). Other options are invalid and cause errors.
Fill both blanks to create a new column with the difference over 3 periods for the 'stock' column.
df['stock_diff'] = df['stock'].diff([1]).fillna([2])
Use diff(3) to get difference over 3 rows. Then fill missing values with 0.0 to avoid NaNs.
Fill all three blanks to create a dictionary comprehension that maps each word to its difference in length compared to the previous word, only if the length difference is positive.
length_diff = {word: len(word) - len(prev) for word, prev in zip(words[[1]:], words[:-[2]]) if (len(word) - len(prev)) [3] 0}The comprehension compares each word with the previous one by shifting the list by 1. It keeps only positive length differences using > 0.