Pandas: Trim Outliers with clip()

Trim extreme numerical outliers in Pandas Series and DataFrames using the clip() method. Set minimum and maximum threshold boundaries to cap values, handle missing thresholds, and clean data distributions.

Introduction to clip() 🔝

The clip() method trims values at specified threshold boundaries. Any values below the lower threshold are set to the lower bound, and any values above the upper threshold are capped at the upper bound.

Create numerical sample dataset with outliers 🔝

We start with a product price Series containing extreme outlier values.
import pandas as pd

prices = pd.Series([10, 45, 120, 50, 500], name="PRICE")
print(prices)
assert prices.max() == 500

Expected output

0     10
1     45
2    120
3     50
4    500
Name: PRICE, dtype: int64

Clipping with lower and upper bounds 🔝

Pass lower and upper parameters to clip() to restrict values within a defined interval.
clipped = prices.clip(lower=20, upper=100)
print(clipped)
assert clipped.max() == 100 and clipped.min() == 20

Expected output

0     20
1     45
2    100
3     50
4    100
Name: PRICE, dtype: int64

Clipping with a single boundary 🔝

You can specify only lower or only upper to apply unilateral threshold capping.
capped_max = prices.clip(upper=80)
print(capped_max)
assert capped_max.max() == 80

Expected output

0    10
1    45
2    80
3    50
4    80
Name: PRICE, dtype: int64

Exercise: cap product prices 🔝

Given a Series scores = pd.Series([25, 45, 85, 95, 110], name="SCORE"), use clip() with a lower bound of 30 and an upper bound of 100. Verify that the maximum value is capped at 100.

Exercise solution 🔝

Solution creating the Series and applying clip().
scores = pd.Series([25, 45, 85, 95, 110], name="SCORE")
answer = scores.clip(lower=30, upper=100)
print(answer)
assert answer.max() == 100 and answer.min() == 30

Expected output

0     30
1     45
2     85
3     95
4    100
Name: SCORE, dtype: int64

Practice in Google Colab 🔝

Interactive Colab Notebook

Practice clip() examples interactively directly in your browser:

Open In Colab View on GitHub

Reference: Pandas clip() 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