🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

Series.str.replace()

AI & DATA SCIENCE // series-str-replace

series.str.replace() substitutes occurrences of a substring or regex pattern with a replacement string, applied across every element of a Series.

Syntax

series.str.replace(pat, repl, regex=False)

Deep Dive Course

By default, str.replace() treats pat as a literal substring to find and replace, but passing regex=True lets pat be a full regular expression pattern instead, enabling more flexible matching, like removing all non-digit characters from a phone number column. Unlike df.replace(), which is meant for replacing whole values throughout a DataFrame, str.replace() specifically operates on substrings within each string value, similar to how Python's own str.replace() method works on a single string.

1Understanding Series.str.replace()

By default, str.replace() treats pat as a literal substring to find and replace, but passing regex=True lets pat be a full regular expression pattern instead, enabling more flexible matching, like removing all non-digit characters from a phone number column. Unlike df.replace(), which is meant for replacing whole values throughout a DataFrame, str.replace() specifically operates on substrings within each string value, similar to how Python's own str.replace() method works on a single string.

💡

Pass regex=True to str.replace() when the pattern to replace is more naturally described as a pattern, like any sequence of digits, than an exact literal substring — without it, pat is always treated as a literal string, even if it looks like it might be a regex.

editor.html
import pandas as pd

s = pd.Series(["555-123-4567", "555.987.6543"])
print(s.str.replace(r"[-.]", "", regex=True))
localhost:3000

2Practical Example

Here is a real-world application of Series.str.replace() showing how it is used in production Pandas code.

editor.html
import pandas as pd

s = pd.Series(["Hello World", "Hello Pandas"])
print(s.str.replace("Hello", "Hi"))
localhost:3000

3Best Practices

Follow these guidelines when working with Series.str.replace():

1. Pass regex=True explicitly when the replacement pattern needs regex features like character classes or quantifiers, not just a literal substring

2. Use str.replace() for substring-level cleanup within text values, reserving df.replace() for replacing whole values across a DataFrame

3. Chain multiple str.replace() calls, or one regex-based call, for multi-step text cleanup, like stripping out several different unwanted characters

⚠️

Tip: Pass regex=True to str.replace() when the pattern to replace is more naturally described as a pattern, like any sequence of digits, than an exact literal substring — without it, pat is always treated as a literal string, even if it looks like it might be a regex.

editor.html
import pandas as pd

s = pd.Series(["555-123-4567", "555.987.6543"])
print(s.str.replace(r"[-.]", "", regex=True))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

s = pd.Series(["555-123-4567", "555.987.6543"])
print(s.str.replace(r"[-.]", "", regex=True))
Example 02Advanced Example
import pandas as pd

s = pd.Series(["Hello World", "Hello Pandas"])
print(s.str.replace("Hello", "Hi"))

Best Practices

  • Pass regex=True explicitly when the replacement pattern needs regex features like character classes or quantifiers, not just a literal substring
  • Use str.replace() for substring-level cleanup within text values, reserving df.replace() for replacing whole values across a DataFrame
  • Chain multiple str.replace() calls, or one regex-based call, for multi-step text cleanup, like stripping out several different unwanted characters

Interview Question

What's the difference between series.str.replace() and df.replace(), given both can substitute values?

Hint: Think about whether each one operates on whole values or on substrings within a value.

series.str.replace() operates at the substring level, finding and replacing a pattern within each string value, similar to editing part of a sentence, and it only works on string, object-dtype, Series. df.replace() instead operates on whole values throughout a DataFrame or Series, substituting an entire matching value with a new one wholesale, and it works across any dtype, not just strings. Wanting to change part of a text field calls for str.replace(); wanting to swap out entire values, like recoding a status code to a full label, calls for df.replace().

Exercises

MediumPractice using Series.str.replace() in a real scenario.
View Solution
import pandas as pd

s = pd.Series(["555-123-4567", "555.987.6543"])
print(s.str.replace(r"[-.]", "", regex=True))

Frequently Asked Questions

What's the difference between series.str.replace() and df.replace(), given both can substitute values?

series.str.replace() operates at the substring level, finding and replacing a pattern within each string value, similar to editing part of a sentence, and it only works on string, object-dtype, Series. df.replace() instead operates on whole values throughout a DataFrame or Series, substituting an entire matching value with a new one wholesale, and it works across any dtype, not just strings. Wanting to change part of a text field calls for str.replace(); wanting to swap out entire values, like recoding a status code to a full label, calls for df.replace().

Related Functions

df-replaceseries-str-containsre-module