The diff() function helps you find how much values change between rows or columns. It shows the difference step-by-step, making it easy to see trends or changes.
To see daily changes in stock prices from a list of prices.
To find how much sales increased or decreased each month.
To calculate the change in temperature from one day to the next.
To analyze the difference in scores between rounds in a game.
To detect sudden jumps or drops in sensor data over time.
Syntax
Pandas
DataFrame.diff(periods=1, axis=0)
periods sets how many steps to look back for difference (default is 1).
axis=0 means difference between rows; axis=1 means difference between columns.
Examples
Finds difference between each row and the previous row (default 1 period, axis=0).
Pandas
df.diff()
Finds difference between each row and the row 2 steps before.
Pandas
df.diff(periods=2)
Finds difference between each column and the previous column for each row.
Pandas
df.diff(axis=1)
Sample Program
This code creates a table of sales over 5 days. Then it adds a new column showing how much sales changed from the previous day.
The first difference value is always NaN because there is no previous row to subtract from.
You can use diff() on both Series and DataFrames.
Negative values mean a decrease, positive values mean an increase.
Summary
diff() shows how values change step-by-step.
Use it to find differences between rows or columns easily.
It helps spot trends, jumps, or drops in your data.
Practice
(1/5)
1. What does the diff() function in pandas do when applied to a DataFrame column?
easy
A. Calculates the difference between each value and the previous value in the column
B. Calculates the sum of all values in the column
C. Sorts the column values in ascending order
D. Replaces missing values with zeros
Solution
Step 1: Understand the purpose of diff()
The diff() function calculates the difference between each element and the one before it in the same column.
Step 2: Compare options with the function's behavior
Only Calculates the difference between each value and the previous value in the column correctly describes this behavior. Other options describe different functions.
Final Answer:
Calculates the difference between each value and the previous value in the column -> Option A
Quick Check:
diff() = difference between consecutive values [OK]
Hint: Remember, diff() shows step-by-step changes in data [OK]
Common Mistakes:
Thinking diff() sums values
Confusing diff() with sorting
Assuming diff() fills missing data
2. Which of the following is the correct syntax to calculate the difference between rows in a pandas DataFrame column named sales?
easy
A. df.diff('sales')
B. df['sales'].diff()
C. df.diff[sales]()
D. df['sales'].difference()
Solution
Step 1: Identify correct method call on a DataFrame column
To calculate differences on a column, call diff() on the Series: df['sales'].diff().
Step 2: Check syntax of each option
df['sales'].diff() uses correct syntax. df.diff('sales') wrongly passes column name to diff. df.diff[sales]() uses wrong brackets. df['sales'].difference() uses a non-existent method.
Final Answer:
df['sales'].diff() -> Option B
Quick Check:
Correct syntax = df['column'].diff() [OK]
Hint: Use diff() directly on the column Series like df['col'].diff() [OK]