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.
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.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
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
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
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.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 clip() examples interactively directly in your browser:
Reference: Pandas clip() documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.