You can perform this task by forming a |-separated string. This works because pd.Series.str.replace accepts regex:

Replace occurrences of pattern/regex in the Series/Index with some other string. Equivalent to str.replace() or re.sub().

This avoids the need to create a dictionary.

import pandas as pd

df = pd.DataFrame({'A': ['LOCAL TEST', 'TEST FOREIGN', 'ANOTHER HELLO', 'NOTHING']})

pattern = '|'.join(['LOCAL', 'FOREIGN', 'HELLO'])

df['A'] = df['A'].str.replace(pattern, 'CORP', regex=True)

#               A
# 0     CORP TEST
# 1     TEST CORP
# 2  ANOTHER CORP
# 3       NOTHING
Answer from jpp on Stack Overflow
Top answer
1 of 6
44

You can perform this task by forming a |-separated string. This works because pd.Series.str.replace accepts regex:

Replace occurrences of pattern/regex in the Series/Index with some other string. Equivalent to str.replace() or re.sub().

This avoids the need to create a dictionary.

import pandas as pd

df = pd.DataFrame({'A': ['LOCAL TEST', 'TEST FOREIGN', 'ANOTHER HELLO', 'NOTHING']})

pattern = '|'.join(['LOCAL', 'FOREIGN', 'HELLO'])

df['A'] = df['A'].str.replace(pattern, 'CORP', regex=True)

#               A
# 0     CORP TEST
# 1     TEST CORP
# 2  ANOTHER CORP
# 3       NOTHING
2 of 6
17

The answer of @Rakesh is very neat but does not allow for substrings. With a small change however, it does.

  1. Use a replacement dictionary because it makes it much more generic
  2. Add the keyword argument regex=True to Series.replace() (not Series.str.replace) This does two things actually: It changes your replacement to regex replacement, which is much more powerful but you will have to escape special characters. Beware for that. Secondly it will make the replace work on substrings instead of the entire string. Which is really cool!
replacement = {
    "LOCAL": "CORP",
    "FOREIGN": "CORP",
    "HELLO": "CORP"
}

dataUS['sec_type'].replace(replacement, regex=True)

Full code example

dataUS = pd.DataFrame({'sec_type': ['LOCAL', 'Sample text LOCAL', 'Sample text LOCAL sample FOREIGN']})

replacement = {
    "LOCAL": "CORP",
    "FOREIGN": "CORP",
    "HELLO": "CORP"
}

dataUS['sec_type'].replace(replacement, regex=True)

Output

0                            CORP
1                            CORP
2                Sample text CORP
3    Sample text CORP sample CORP
Name: sec_type, dtype: object
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 3.0.5 documentation
Method to replace occurrences of a substring with another substring. ... Extract substrings using a regular expression. ... Find all occurrences of a pattern or regex in each string.
Discussions

Vectorized .str.replace() for multiple characters in pandas
I think my count and year columns are breaking because of the if statement excluding those two columns. More on reddit.com
🌐 r/learnpython
3
1
June 9, 2022
python - Pandas: replace substring in string - Stack Overflow
When I try this on a dataframe it no longer finds substrings. 2020-12-31T15:25:12.307Z+00:00 ... For me this works for both columns and whole dataframes (without .str), for Pandas v2.2.2. 2024-06-15T12:31:08.09Z+00:00 ... Voting is disabled while the site is in read-only mode. ... Voting is disabled while the site is in read-only mode. Saving is disabled while the site is in read-only mode. ... Show activity on this post. use str.replace ... More on stackoverflow.com
🌐 stackoverflow.com
python - pandas dataframe replace multiple substring of column - Stack Overflow
What I am trying to remove every ... to replace with blank.. ... Md. Parvez AlamMd. Parvez Alam · 4,46655 gold badges5454 silver badges110110 bronze badges ... import pandas as pd df = pd.DataFrame({'A': ['$5,756', '3434', '$45', '1,344']}) df['A'] = df['A'].str.replace('[$,]', ... More on stackoverflow.com
🌐 stackoverflow.com
July 27, 2022
python 2.7 - Pandas dataframe replace string in multiple columns by finding substring - Stack Overflow
I have a very large pandas data frame containing both string and integer columns. I'd like to search the whole data frame for a specific substring, and if found, replace the full string with something else. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › Series › str › replace
Python Pandas Series str replace() - Replace Substring | Vultr Docs
November 26, 2024 - The result will reflect the changes wherever 'foo' appears. Occasionally, you'll need to replace more than one specific substring. Use the replace() method with a dictionary to specify multiple replacements.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas replace substring in dataframe
Pandas Replace Substring in DataFrame - Spark By {Examples}
June 6, 2025 - You can find how to replace substrings in a pandas DataFrame column using the replace() method with lambda functions. This versatile method allows you to
🌐
Reddit
reddit.com › r/learnpython › vectorized .str.replace() for multiple characters in pandas
r/learnpython on Reddit: Vectorized .str.replace() for multiple characters in pandas
June 9, 2022 -

I have a dataframe:

 {'country': {0: 'Afghanistan?*', 1: 'Albania?*'},
 'region': {0: 'Asia', 1: 'Europe'},
 'subregion': {0: 'Southern Asia', 1: 'Southern Europe'},
 'rate_per_1000': {0: 6.7, 1: 2.1},
 'count': {0: '2,474', 1: '61'},
 'year': {0: 2018, 1: 2020},
 'source': {0: 'NSO', 1: 'NSO'}}

          country  region        subregion  rate_per_1000  count  year source
0   Afghanistan?*    Asia    Southern Asia            6.7  2,474  2018    NSO
1       Albania?*  Europe  Southern Europe            2.1     61  2020    NSO

There are multiple bad characters here that I want to get rid of. I made a short function for .apply() to get rid of them, however I am looping over a defined list of bad characters. This gives a bad code smell to me, I think this operation could be more vectorized in some way. This is what I've tried:

bad_chars = ['?', '*', ',']

def string_cleaner(col):
    if col.dtype == 'object':
        for char in bad_chars:
            col = col.str.replace(f'{char}', '')
        return col

homicide_by_country = homicide_by_country.apply(string_cleaner)

homicide_by_country
        country  region        subregion rate_per_1000 count  year source
0   Afghanistan    Asia    Southern Asia          None  2474  None    NSO
1       Albania  Europe  Southern Europe          None    61  None    NSO

My desired outcome is a more pythonic/pandonic technique for accomplishing the same outcome.

You may notice for some reason my rate_per_1000 columns goes blank. I haven't troubleshot that problem yet but if you spot something obvious I'm all ears.

🌐
DataScience Made Simple
datasciencemadesimple.com › home › replace substring/pattern of column in pandas python
Replace substring/pattern of column in pandas python - DataScience Made Simple
July 28, 2023 - A substring Zona is replaced with another string Arizona. So the resultant dataframe will be · The Substring India is replaced with Bharat, using replace function with regex=True argument as shown below.
🌐
Programiz
programiz.com › python-programming › pandas › methods › series-str-replace
Pandas str.replace() (With Examples)
import pandas as pd # create a Series cities = pd.Series(['San Jose', 'Los Angeles', 'San Francisco']) # use str.replace() to replace 'San' with 'Santa' cities = cities.str.replace('San', 'Santa') print(cities) ... In the above example, we have used the str.replace('San', 'Santa') method to replace the substring San with Santa in each string of the cities Series.
Find elsewhere
🌐
pythontutorials
pythontutorials.net › blog › replace-multiple-substrings-in-a-pandas-series-with-a-value
How to Replace Multiple Substrings in a Pandas Series with a Single Value: Step-by-Step Guide to Fixing the List Error — pythontutorials.net
By the end, you’ll confidently clean text data in Pandas Series without breaking a sweat. Understanding the Problem: Replacing Multiple Substrings ... Suppose you’re analyzing a dataset of product names with typos or variations. For example: Your goal is to replace all misspellings of "iPhone" (e.g., "iphon", "iphonex", "ipnone") with the standardized "iPhone". A natural first thought is to pass all problematic substrings as a list to str.replace(), like this:
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › python-pandas-series-str-replace-to-replace-text-in-a-series
Python | Pandas Series.str.replace() to replace text in a series - GeeksforGeeks
July 11, 2025 - Example: The .str.replace() method is a part of the Pandas String Handling capabilities. This let users to replace occurrences of a specified substring with another substring in text data contained within a Pandas Series.
🌐
Statology
statology.org › home › how to use str.replace in pandas (with examples)
How to Use str.replace in Pandas (With Examples)
April 11, 2024 - “`python df[‘columnname’] = df[‘columnname’].str[19:24].replace(‘columnname’, ‘columnname’) “` It seems the `.replace()` part may not be needed unless you’re replacing specific values within those extracted substrings.
🌐
Linux Hint
linuxhint.com › pandas-str-replace
Linux Hint – Linux Hint
September 21, 2022 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
GeeksforGeeks
geeksforgeeks.org › python › replace-characters-in-strings-in-pandas-dataframe
Replace Characters in Strings in Pandas DataFrame - GeeksforGeeks
July 23, 2025 - In this article, we are going to see how to replace characters in strings in pandas dataframe using Python.
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas documentation
Method to replace occurrences of a substring with another substring. ... Extract substrings using a regular expression. ... Find all occurrences of a pattern or regex in each string.
🌐
w3tutorials
w3tutorials.net › blog › replace-part-of-the-string-in-pandas-data-frame
How to Replace Part of a String in a Pandas DataFrame: Fixing pd.replace() Limitations — w3tutorials.net
Here, regex=True treats "apple" as a regex pattern and replaces any element containing "apple" with "pear"—not just the substring "apple". This is rarely desired for partial replacement. The fix? Use Pandas’ str.replace() method, designed explicitly for string operations on Series.
🌐
Towards Data Science
towardsdatascience.com › home › latest › 2 different replace functions of python pandas
2 Different Replace Functions of Python Pandas | Towards Data Science
January 20, 2025 - ... The replace function available via the str accessor can be used for replacing a part or subsequence of a string. Accessors in Pandas provide functions specific to a particular data type.
🌐
Quora
quora.com › How-can-I-replace-characters-in-a-multiple-column-name-in-pandas
How to replace characters in a multiple column name in pandas - Quora
How do I replace multiple substrings in column names using a dictionary in Pandas? How do I get the column name based on values in the column in Pandas? ... How do I replace '..' and '.' with single periods and question marks in pandas? Df ['column'].str.replace is not working.