Bound extreme values
Normalize numeric inputs before downstream calculations.
Pandas · Module
DataFrame.clip constrains values to optional lower and upper thresholds, replacing out-of-range entries with the relevant boundary. Scalar or array-like thresholds are supported.
DataFrame.clip returns a DataFrame whose values are constrained by optional lower and upper boundaries. Scalar thresholds apply broadly; array-like thresholds are applied element-wise along the selected axis, and missing thresholds leave corresponding values unchanged.
DataFrame.clip(lower=None, upper=None, *, axis=None, inplace=False, **kwargs)| Name | Type | Description |
|---|---|---|
lower | float or array-like, default None | Minimum threshold; values below it are replaced by it. Missing thresholds do not clip values. |
upper | float or array-like, default None | Maximum threshold; values above it are replaced by it. Missing thresholds do not clip values. |
axis | {0 or 'index', 1 or 'columns', None}, default None | Aligns the object with lower and upper along the chosen axis. |
inplace | bool, default False | Whether to perform the operation in place. |
**kwargs | additional keyword arguments | Additional NumPy compatibility keyword arguments. |
Series or DataFrame: same type as the calling object with values outside the clip boundaries replaced.
import pandas as pd
df = pd.DataFrame({"a": [-3, 1, 7], "b": [0, 5, 9]})
result = df.clip(lower=0, upper=6)
print(result)Replaces values below 0 with 0 and values above 6 with 6.
Normalize numeric inputs before downstream calculations.
Apply different limits by row or column.
It replaces values below lower with lower and values above upper with upper. Either boundary can be omitted by leaving it as None.
Follow-up: How do array-like thresholds behave? They are applied element-wise along the axis selected by axis.
With scalar thresholds, the same bounds are used across the DataFrame. Array-like thresholds provide distinct bounds, and axis controls how those bounds align.
Follow-up: What happens when axis is omitted? Alignment uses the default behavior specified by pandas for the object and supplied thresholds.
The method returns the same kind of object as the caller with out-of-bound values replaced. The documented signature defaults inplace to False.
Follow-up: What should code expect when mutating in place? It should account for the in-place operation rather than relying on a returned clipped object.
pandas.Series.clippandas.DataFrame