Use replace with dict for replacing and regex=True:

df['url'] = df['url'].replace({'icashier.alipay.com': 'aliexpress.com'}, regex=True)
print (df)
                                          url
0  aliexpress.com/catalog/2758186/detail.aspx
1  aliexpress.com/catalog/2758186/detail.aspx
2  aliexpress.com/catalog/2758186/detail.aspx
3                                      vk.com
Answer from jezrael on Stack Overflow
🌐
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.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.replace.html
pandas.Series.replace — pandas 3.0.5 documentation
Replace values based on boolean condition. ... Apply a function to a Dataframe elementwise. ... Map values of Series according to an input mapping or function. ... Simple string replacement.
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
str.replace('.','') should replace every character?
Hi everyone, I was showing str.replace ... pd.Series.str.replace('.','') I expected every character to be removed but instead nothing happened. Is that a bug or is there something I don't understand? ... commit: None python: 3.6.5.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 85 Stepping 4, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None · pandas: 0.23.0 pytest: ... More on github.com
🌐 github.com
10
January 16, 2019
Str.replace method and dataframe or series.replace
Hi everyone. I have read the method ‘replace’ for DataFrames and series without accessing series.str. As far as I understood, they do the same thing as series.str.replace function and changes the strings in a column. But… More on community.dataquest.io
🌐 community.dataquest.io
6
0
December 1, 2020
python - Replace multiple substrings in a Pandas series with a value - Stack Overflow
You can perform this task by forming a |-separated string. This works because pd.Series.str.replace accepts regex: More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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.

🌐
GitHub
github.com › pandas-dev › pandas › issues › 24804
str.replace('.','') should replace every character? · Issue #24804 · pandas-dev/pandas
January 16, 2019 - Code Sample (pandas 0.23.0) In [1]: import pandas as pd s = pd.Series(['abc','123']) s.str.replace('.','',regex = True) Out [1]: 0 abc 1 123 dtype: object Problem description Hi everyone, I was showing str.replace to a colleague and how ...
Author: pandas-dev
Find elsewhere
🌐
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.1.0.dev0 documentation
Replace values based on boolean condition. ... Apply a function to a Dataframe elementwise. ... Map values of Series according to an input mapping or function. ... Simple string replacement.
🌐
Programiz
programiz.com › python-programming › pandas › methods › series-str-replace
Pandas str.replace() (With Examples)
The str.replace() method is used to replace a substring within each string element of a Series with another string. The str.replace() method in Pandas is used to replace a substring within each string element of a Series with another string.
🌐
Dataquest Community
community.dataquest.io › q&a
Str.replace method and dataframe or series.replace - Q&A - Dataquest Community
December 1, 2020 - Hi everyone. I have read the method ‘replace’ for DataFrames and series without accessing series.str. As far as I understood, they do the same thing as series.str.replace function and changes the strings in a column. But when I used series.replace method instead of series.str.replace, it did not do the required action.
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 › pandas-docs › version › 2.0 › reference › api › pandas.Series.str.replace.html
pandas.Series.str.replace — pandas 2.0.3 documentation
Replace each occurrence of pattern/regex in the Series/Index. Equivalent to str.replace() or re.sub(), depending on the regex value.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.0.5 documentation
Replace values based on boolean condition. ... Apply a function to a Dataframe elementwise. ... Map values of Series according to an input mapping or function. ... Simple string replacement.
🌐
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 output is the same except for the data type. When the "str.replace" is used, the data type remains as string (or object). Thus, we need an extra step of data type conversion to have integers representing categories. We have learned two different replace function of Pandas and how they differ.
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › DataFrame › replace
Python Pandas DataFrame replace() - Replace Values | Vultr Docs
December 27, 2024 - The value 1 in column 'A' and 5 in column 'B' are replaced with 99. This method allows selective replacement within specific columns. Assemble a DataFrame with string values.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 16808
Series.str.replace() is not actually the same as ...
June 30, 2017 - In [1]: import pandas as pd In [2]: series = pd.Series(['a', '(b)']) In [3]: series.str.replace('a', '[a]') Out[3]: 0 [a] 1 (b) dtype: object In [4]: series.str.replace('(b)', '[b]') # unexpected behavior Out[4]: 0 a 1 ([b]) dtype: object In [5]: series.str.replace('\(b\)', '[b]') # need to escape Out[5]: 0 a 1 [b] dtype: object In [6]: '(b)'.replace('(b)', '[b]') # Python str.replace is different, uses literal string Out[6]: '[b]' The documentation for Series.str.replace says that it takes a "string or compiled regex" ...
Author: pandas-dev
🌐
Reddit
reddit.com › r/learnpython › pandas .replace not working
r/learnpython on Reddit: pandas .replace not working
December 31, 2015 -

This is genuinely driving me crazy.

I have a data frame of unit prices in string format i'm trying to get them to a float

item_df['Unit Price'] = item_df['Unit Price'].replace('$','')

and all the '$' are still there.

THEN when I do this:

item_df['Unit Price'][1] = item_df['Unit Price'][1].replace('$','')

The '$' is gone from that index ಠ_ಠ. What the hell is going on?? Am I taking crazy pills or missing some fundamental concept?

Any help would be much appreciated.

Thanks,

🌐
GeeksforGeeks
geeksforgeeks.org › pandas › replace-values-in-pandas-dataframe-using-regex
Replace Values in Pandas Dataframe using Regex - GeeksforGeeks
October 9, 2025 - Explanation: The regex [nN]ew matches both "New" and "new", replacing them with "New_" across the entire DataFrame column. The apply() function lets you define a custom function that uses Python’s re module for pattern matching and string replacement.
🌐
Pandas
pandas.pydata.org › docs › user_guide › text.html
Working with text data — pandas 3.0.5 documentation
Changed in version 3.0: The inference and behavior of strings changed significantly in pandas 3.0.