Replacing Values: replace, where & mask

Replacing is how you swap unwanted values for better ones — by exact match with replace(), or by condition with where() and mask().

Learn Replacing Values: replace, where & mask in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise…

Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Learn to map values with replace() (scalar, dict, and regex), keep-or-substitute with where(), replace-where-true with mask(), and cap outliers with clip().

What You'll Learn in This Lesson

1 Swapping Values — Series.replace()

replace() matches values and substitutes them. In its simplest form you give one old value and one new value. The dictionary form is where it shines: a single call can map many old values to new ones — perfect for expanding state codes, fixing common typos, or turning placeholder strings like "N/A" into real NaN .

2 Conditional Replace — where() vs mask()

When the replacement depends on a condition rather than a known value, use where and mask — and they are mirror images. df.where(cond, other) keeps the value where cond is True and fills other everywhere it is False. df.mask(cond, other) does the opposite: it replaces with other where cond is True.

3 Capping Outliers — clip(lower, upper)

clip() squeezes every value into a range. Anything below lower is raised to lower , anything above upper is lowered to upper , and values already inside the range are untouched. It is the one-liner for taming outliers — forcing a percentage column to stay between 0 and 100, or capping a noisy sensor reading.

! Common Errors (And How to Fix Them)

✅ Fix: where keeps where True — flip the test or use mask:

The result was not assigned back (replace is not in place by default):

🎯 Mini Challenge: Clean a Readings Column

❓ Frequently Asked Questions

Lesson complete — you can reshape any value!

You can swap exact values with replace (scalar, dict, regex), apply conditional logic with where and mask , and clamp ranges with clip .

🚀 Up next: Checkpoint — Cleaning & Wrangling — put every cleaning skill together on one messy dataset.

Practice quiz

What does s.replace(2, 99) do to the Series [1, 2, 3]?

replace swaps every 2 for 99, giving [1, 99, 3].

Which method maps each value through a dictionary?

  • s.translate()
  • s.map()
  • s.swap()
  • s.lookup()

Answer: s.map(). Series.map({'a': 'X'}) maps each value via the dict.

After s.where(s > 1, 0) on [1, 2, 3], what is the result?

where KEEPS values where the condition is True and replaces the rest, giving [0, 2, 3].

How does mask() differ from where()?

  • mask only works on strings
  • mask sorts the data first
  • mask is faster but identical
  • mask replaces where the condition is True (the opposite of where)

Answer: mask replaces where the condition is True (the opposite of where). mask replaces values where the condition is True; where replaces where it is False.

What does s.replace([1, 2], 0) do?

  • Replaces both 1 and 2 with 0
  • Replaces only the first match
  • Raises a TypeError
  • Adds a new value 0

Answer: Replaces both 1 and 2 with 0. A list of targets maps all of them to the single replacement 0.

With map(), what happens to a value missing from the dict?

  • It keeps its original value
  • It becomes NaN
  • It raises a KeyError
  • It becomes 0

Answer: It becomes NaN. Unmapped values become NaN with map(); use replace() to keep originals.

Which method keeps the original value when no replacement is found?

  • s.map()
  • s.factorize()
  • s.replace()
  • s.dropna()

Answer: s.replace(). replace() leaves unmatched values unchanged, unlike map().

What does s.mask(s > 1, 0) give for [1, 2, 3]?

mask replaces values where True (the 2 and 3), giving [1, 0, 0].

Which argument lets replace() use regular expressions?

  • regex=True
  • pattern=True
  • re=True
  • match=True

Answer: regex=True. Pass regex=True so replace interprets the target as a pattern.

How do you replace NaN values specifically?

  • s.replace(NaN, 0)
  • s.dropna(0)
  • s.fillna(0)
  • s.map(0)

Answer: s.fillna(0). fillna(0) is the idiomatic way to replace missing values.

Continue this course