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

Pandas · Module

df.mask

DataFrame.mask replaces values where a condition is True and keeps values where it is False. Replacement values come from other, or from dtype-appropriate nulls when other is omitted.

How it works

DataFrame.mask evaluates a boolean condition element by element. Values where the condition is False remain unchanged; values where it is True come from other, or become the dtype-appropriate missing value when other is omitted. The operation returns a DataFrame unless inplace=True.

Syntax

DataFrame.mask(cond, other=<no_default>, *, inplace=False, axis=None, level=None)

Parameters

NameTypeDescription
condbool Series/DataFrame, array-like, or callableBoolean Series/DataFrame, array-like, or callable selecting entries to replace; False entries are retained.
otherscalar, Series/DataFrame, or callableScalar, Series/DataFrame, or callable supplying replacement values; omitted values become dtype-appropriate nulls.
inplacebool, default FalseWhether to perform the operation in place.
axisint, default NoneAlignment axis when needed.
levelint, default NoneAlignment level when needed.

Returns

A DataFrame when called on a DataFrame; None when inplace=True.

Key points

  • mask selects positions whose condition evaluates to True and replaces only those positions.
  • False condition entries retain their original DataFrame values.
  • A callable condition is evaluated on the DataFrame and must return boolean-shaped output.
  • The replacement can come from a scalar or corresponding values in another aligned object.
  • Leaving other unspecified fills selected entries with a dtype-appropriate null.
  • Set inplace=True to request mutation instead of receiving a returned DataFrame.

Common mistakes

  • A callable supplied as cond or other must not mutate the DataFrame it receives.
  • Misaligned condition and replacement labels can cause those positions to be replaced, so align inputs deliberately.
  • The original object's dtype influences how replacement values are represented.

Example: Replace values matching a condition

import pandas as pd

scores = pd.DataFrame({"math": [72, 91], "science": [88, 64]})
result = scores.mask(scores < 70, other=0)
print(result)

Replaces every score below 70 with zero while leaving all other values unchanged.

When to use it

Threshold-based cleaning

Sanitize tabular measurements by replacing values that violate a threshold.

Conditional redaction

Redact selected cells while preserving the rest of a DataFrame.

Conditional replacement

Apply a scalar, aligned object, or computed replacement to matching entries.

Interview questions

QWhat does DataFrame.mask do?

mask replaces elements where cond is True and preserves elements where cond is False. With a DataFrame input, it returns a DataFrame unless inplace=True.

Follow-up: How does mask differ from where? where replaces values where the condition is False, while mask replaces values where it is True.

QWhat forms can cond take?

cond can be a boolean Series, DataFrame, array-like object, or callable. A callable is evaluated against the DataFrame and must produce a boolean Series, DataFrame, or array.

Follow-up: What restriction applies to that callable? It must not modify the input DataFrame, even though pandas does not validate this.

QHow is the replacement value chosen?

other may be a scalar, Series, DataFrame, or callable. If omitted, positions selected by cond are filled with the appropriate null value, such as np.nan for NumPy dtypes or pd.NA for extension dtypes.

Follow-up: What happens when replacement labels do not align with the condition? Misaligned condition positions are treated as True and therefore are replaced.

  • DataFrame.where
  • Series.mask
  • Series.where

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