Pandas str.replace multiple values in Python [3 Examples]

In this Python tutorial, I will explain how Pandas str.replace multiple values in Python works with some illustrative examples.

To replace multiple values in strings within pandas DataFrames in Python, the str.replace() method can be effectively used with different strategies, such as chaining it with another str.replace() for sequential replacements, and employing regular expressions (regex) for more complex, pattern-based substitutions.

Pandas str.replace() function in Python

The str.replace method in Pandas is used to replace a string or regular expression pattern with another string. It’s a part of the string methods under Pandas, accessible through the str accessor.

The basic syntax of Pandas str.replace function is as follows:

Series.str.replace(pat, repl, n=-1, case=None, flags=0, regex=True)

Here,

patString or regular expression pattern to replace.
replString to replace with.
nNumber of replacements to make from the start.
caseDetermines if case sensitive.
flagsRegex module flags (e.g., re.IGNORECASE).
regexIf True, assumes the pattern to be a regular expression.
List of the parameters required in the str.replace() function in Pandas.

Here’s a simple example of using Pandas str.replace in Python:

import pandas as pd

states = pd.Series(['CA', 'WA', 'AZ', 'MA'])
print("Before Replacing:\n", states)

states_replaced = states.str.replace('A', '@')
print("After Replacing:\n", states_replaced)

Output:

Before Replacing:
 0    CA
1    WA
2    AZ
3    MA
dtype: object
After Replacing:
 0    C@
1    W@
2    @Z
3    M@
dtype: object

Upon the completion of code execution in Pycharm, the resulting output is depicted in the screenshot below.

replace multiple values in a column pandas Python

Pandas str.replace multiple values in Python

To replace multiple values in Python Pandas, we can chain the str.replace method or use regular expressions.

1. str.replace Pandas multiple values by chaining

This approach involves using Pnadas str.replace multiple values in Python by applying the Pandas str.replace function multiple times in succession. Each str.replace call replaces one set of values with another.

import pandas as pd

cities = pd.Series(['Atlanta', 'Seattle', 'Phoenix', 'Boston'])
print("Before Replacing:\n", cities)

cities_replaced = cities.str.replace('a', '@').str.replace('e', '3')
print("After Replacing:\n", cities_replaced)

Output:

Before Replacing:
 0    Atlanta
1    Seattle
2    Phoenix
3     Boston
dtype: object
After Replacing:
 0    Atl@nt@
1    S3@ttl3
2    Pho3nix
3     Boston
dtype: object

The screenshot displayed below illustrates the output after the code has been processed in the Pycharm editor.

Pandas str.replace multiple values in Python

2. df.str.replace multiple values using regular expressions

In this method, we utilize regular expressions within str.replace to match and replace multiple different patterns in one go.

Here is the code, that demonstrates how Pandas str.replace multiple values in Python using regular expressions:

import pandas as pd

descriptions = pd.Series(['California beaches', 'Disney in Florida', 'Snow in Colorado'])
print("Before Replacing:\n", descriptions)

descriptions_replaced = descriptions.str.replace(r'(California|Florida)', 'Sunshine State', regex=True)
print("After Replacing:\n", descriptions_replaced)

Output:

Before Replacing:
 0    California beaches
1     Disney in Florida
2      Snow in Colorado
dtype: object
After Replacing:
 0      Sunshine State beaches
1    Disney in Sunshine State
2            Snow in Colorado
dtype: object

Following the execution of the code in the Pycharm editor, the subsequent screenshot captures the result.

pandas str replace multiple values in Python

Conclusion

Here, I have explained how to use the Pandas str.replace() method in Python for modifying string data, covering basic string replacement, chaining str.replace() for multiple values, and integrating regular expressions for more complex substitutions.

These examples highlight the method’s adaptability in various scenarios, providing an essential tool for efficient and precise string manipulation within pandas DataFrames.

You may also like to read: