October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Replace Multiple Values in a Pandas DataFrame with str.replace()

Use a dictionary with Series.str.replace() to make several different substring substitutions in one pandas column, or choose DataFrame.replace() to remap whole-cell values.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To replace several substrings in one pandas column, call .str.replace() on that column and assign the returned Series back to it. In pandas 3.0.6, you can pass a dictionary of pattern-to-replacement pairs: df["col"] = df["col"].str.replace({"old1": "new1", "old2": "new2"}). Use regex=False for literal text or regex=True for regular expressions.

Replace multiple substrings with different text

A DataFrame column is a Series, so select the column before using its .str accessor. The pandas 3.0.6 API accepts a dictionary as pat; each key is a pattern and its value is the replacement. When pat is a dictionary, leave repl as None—the dictionary supplies the replacements.

df["col"] = df["col"].str.replace({"foo": "bar", "baz": "qux"})

This applies the corresponding substitutions to string values in the selected column. The method returns a transformed Series or Index; assigning it to df["col"] makes the result the DataFrame’s column value. See the pandas Series.str.replace API reference.

Choose literal matching or regular expressions

Match text literally

In the current Series API, regex=False is the default, so string patterns are treated as literal text. To make that intent explicit, write:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
df["col"] = df["col"].str.replace("foo", "bar", regex=False)

Match several alternatives with one replacement

If all alternatives should be replaced by the same text, combine them in one regular expression and set regex=True:

df["col"] = df["col"].str.replace(r"foo|baz", "replacement", regex=True)

Here, either foo or baz is replaced with replacement. Use a dictionary instead when each pattern needs its own replacement. The pandas text-data guide notes that, from pandas 2.0, a single-character pattern with regex=True is treated as a regular expression too.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use DataFrame.replace for whole-cell values

DataFrame.replace() is the better fit when you want to remap cell values rather than edit matching text within strings. For example, this maps a whole cell value:

df = df.replace({"old": "new"})

The DataFrame method has its own argument forms for scalar, list, dictionary, nested-dictionary, and regex replacement. It can target column-specific values with nested mappings; consult the DataFrame.replace API reference for the appropriate mapping shape. Its argument behavior and defaults are separate from those of Series.str.replace().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Method Best for Scope Pattern behavior
df["col"].str.replace(...) Changing substrings inside text A selected Series (column) regex=False by default; set regex=True for regex matching. A dictionary pattern maps patterns to separate replacement strings.
df.replace(...) Remapping whole cell values or applying DataFrame replacement rules DataFrame cells, with column-specific forms available Uses its own to_replace, value, and regex argument forms; see the DataFrame API reference.

Common mistakes to avoid

  • Calling .str.replace() on the DataFrame. Select a Series first, such as df["col"]; the usual string-accessor call operates on a Series or Index.
  • Passing a separate replacement with a dictionary pattern. The dictionary already contains the replacement for each key, so repl must remain None.
  • Forgetting to keep the returned result. Assign the transformed Series back to the column when you want the DataFrame to retain the edits.
  • Assuming missing values become strings. The official Series examples show missing values unchanged.
  • Applying Series defaults to DataFrame.replace. The two methods have different argument forms and defaults; choose based on whether the target is text inside a cell or the cell value itself.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.