Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.clip

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.

How it works

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.

Syntax

DataFrame.clip(lower=None, upper=None, *, axis=None, inplace=False, **kwargs)

Parameters

NameTypeDescription
lowerfloat or array-like, default NoneMinimum threshold; values below it are replaced by it. Missing thresholds do not clip values.
upperfloat or array-like, default NoneMaximum threshold; values above it are replaced by it. Missing thresholds do not clip values.
axis{0 or 'index', 1 or 'columns', None}, default NoneAligns the object with lower and upper along the chosen axis.
inplacebool, default FalseWhether to perform the operation in place.
**kwargsadditional keyword argumentsAdditional NumPy compatibility keyword arguments.

Returns

Series or DataFrame: same type as the calling object with values outside the clip boundaries replaced.

Key points

  • Use clip to constrain DataFrame values to lower and upper limits.
  • Out-of-range entries become exactly the boundary value rather than being removed.
  • Both scalar and array-like limits are supported.
  • Array-like bounds are applied per element according to the selected axis.
  • The lower bound raises smaller values to the minimum.
  • The upper bound lowers larger values to the maximum.

Common mistakes

  • Missing lower or upper thresholds do not alter the corresponding values.
  • Use inplace=True only when mutation is intended; otherwise the method returns a clipped DataFrame.

Example: Clip DataFrame values to a range

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.

When to use it

Bound extreme values

Normalize numeric inputs before downstream calculations.

Use aligned array-like thresholds

Apply different limits by row or column.

Interview questions

QWhat does DataFrame.clip do?

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.

QHow are thresholds aligned?

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.

QWhat does clip return?

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.clip
  • pandas.DataFrame

Source: pandas.DataFrame.clip. Checked 2026-10-15.