Pandas: Calculate Discrete Differences with diff()

Compute discrete differences between current and prior elements across Series and DataFrames using the Pandas diff() method. Measure period-over-period changes, calculate velocity or acceleration in time series data, and configure custom lag periods.

Introduction to diff() 🔝

The diff() method calculates the absolute difference between an element and a prior element (specified by the periods parameter). By default, it subtracts the immediately preceding row (`periods=1`). The first row naturally evaluates to `NaN`.

Create numerical sample series 🔝

We start with a numerical Series representing sequential measurements or stock prices.
import pandas as pd

s = pd.Series([10, 15, 12, 18], name="VAL")
print(s)
assert s.shape == (4,)

Expected output

0    10
1    15
2    12
3    18
Name: VAL, dtype: int64

Basic discrete difference calculation 🔝

Call diff() on the Series to compute row-over-row differences.
res = s.diff()
print(res)
assert res.iloc[1] == 5.0 and res.iloc[2] == -3.0

Expected output

0    NaN
1    5.0
2   -3.0
3    6.0
Name: VAL, dtype: float64

Configuring lag periods 🔝

Use the periods parameter to calculate differences across multiple rows (e.g., comparing against two periods prior).
res_lag = s.diff(periods=2)
print(res_lag)
assert res_lag.iloc[2] == 2.0

Expected output

0    NaN
1    NaN
2    2.0
3    3.0
Name: VAL, dtype: float64

Differencing DataFrames across rows or columns 🔝

When applied to a DataFrame, diff() computes row-wise differences (axis=0) by default. Set axis=1 to calculate column-wise differences.
df = pd.DataFrame({
    "A": [10, 20, 30],
    "B": [15, 25, 40]
})
df_diff = df.diff(axis=1)
print(df_diff)
assert df_diff.loc[0, "B"] == 5.0

Expected output

     A    B
0  NaN  5.0
1  NaN  5.0
2  NaN  10.0

Exercise: compute daily temperature changes 🔝

Given a Series temps = pd.Series([30, 32, 31, 35], name="TEMP"), compute the discrete difference using diff() and verify that the difference at index 3 equals 4.0.

Exercise solution 🔝

Solution creating the Series and applying diff().
temps = pd.Series([30, 32, 31, 35], name="TEMP")
answer = temps.diff()
print(answer)
assert answer.iloc[3] == 4.0

Expected output

0    NaN
1    2.0
2   -1.0
3    4.0
Name: TEMP, dtype: float64

Practice in Google Colab 🔝

Interactive Colab Notebook

Practice diff() examples interactively directly in your browser:

Open In Colab View on GitHub

Reference: Pandas diff() documentation.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer