Series.replace()

noun

say it: “SEER-eez dot ree-PLAYSS” /ˈsɪriz dɑt rɪˈpleɪs/

A pandas method that swaps the whole values you name for new ones, such as fixing a few misspellings, and leaves every other value as it was.

What does Series.replace() do in pandas?

Series.replace(), given a dictionary of old value to new value, changes only the whole values the dictionary names and leaves every other value as it was. It suits fixing a few known misspellings, or turning a stand-in such as -999 into NaN.

import pandas as pd
sizes = pd.Series(["M", "med", "L", "lg"])
print(sizes.replace({"med": "M", "lg": "L"}))   # M, M, L, L

In a sentence

  • “Series.replace() fixed "med" and "lg" and left the other sizes alone.”
  • “She used Series.replace() to turn every -999 into NaN.”

Level: Journeyman · More Python terms at this level

Updated