Use regex (seperate the strings by |):

df['schoolname'] = df['schoolname'].str.replace('high|school', "")
Answer from Andy Hayden on Stack Overflow
🌐
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.

🌐
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
To replace characters in multiple column names in pandas, use vectorized string methods on the DataFrame’s .columns (an Index) or rename with a mapping/func.
Discussions

python - pandas replace (erase) different characters from strings - Stack Overflow
As you can see, the years were entered using multiple formats (ugh!) with brackets and copyright symbols and lowercase c and uppercase C. Now I wanted to remove those unwanted characters and only have the years in four digits. Since it's an array, you also need to transform it into a string before using replace... More on stackoverflow.com
🌐 stackoverflow.com
python - How to replace multiple character in string of data frame in pandas? - Stack Overflow
I have such a data frame: #v1 v2 v3 v4 v5 a b b c 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 ... .... As you see, the v5 column contains word id. And I have a list of word id to remove: More on stackoverflow.com
🌐 stackoverflow.com
python - Replace multiple characters across all columns pandas df - Stack Overflow
My df has 200+ columns. Some of the column names contain special characters like: () [] I need to replace all 3 with _. I also need to replace empty spaces as well... Here is my code: import pandas... More on stackoverflow.com
🌐 stackoverflow.com
April 6, 2021
python - pandas string replace multiple character in a cell - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › pandas-replace-multiple-values-in-python
Pandas Replace Multiple Values in Python - GeeksforGeeks
July 23, 2025 - Ways to Replace Multiple Values in Python Using Pandas are: ... One of Pandas most useful tools is the replace() method, which allows to substitute desired values with specified ones.
🌐
Google Groups
groups.google.com › g › pydata › c › GVDNJCvjuXw
how to replace characters in multiple column name in pandas
June 9, 2018 - df_hld.columns = df_hld.columns.str.replace("hld", "pct") A similar question with helpful answers: https://stackoverflow.com/q/38299205 ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... but this will replace the characters in all of my columns names.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.0.5 documentation
Map values of Series according to an input mapping or function. ... Simple string replacement. ... Regex substitution is performed under the hood with re.sub. The rules for substitution for re.sub are the same. Regular expressions will only substitute on strings, meaning you cannot provide, for example, a regular expression matching floating point numbers and expect the columns in your frame that have a numeric dtype to be matched.
🌐
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 "eng" string in the profession column should be replaced with "engineer". Thanks to the flexibility of Pandas, we can do both replacements in a single operation. Each replacement is written as a key-value pair in the dictionary. ... Both "doc" and "eng" have been replaced. There is another way of replacing multiple values in a column, which is using Python lists to indicate values to be replaced and the new ones.
Find elsewhere
🌐
IncludeHelp
includehelp.com › python › pandas-replace-a-character-in-all-column-names.aspx
Python - Pandas replace a character in all column names
July 30, 2022 - To replace a character in all column names in pandas DataFrame, you can use the df.columns.str.replace() method by specifying the old and new character to be replaced as the parameters of the function.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Replace values in DataFrame and Series with replace() | note.nkmk.me
January 17, 2024 - In pandas, the replace() method allows you to replace values in DataFrame and Series. It is also possible to replace parts of strings using regular expressions (regex). pandas.DataFrame.replace — pan ...
🌐
Python Guides
pythonguides.com › pandas-replace-multiple-values
Replace Multiple Values In Pandas DataFrame Using Str.Replace()
May 22, 2025 - # Replace values across the entire DataFrame df_replaced = df.replace({ 'California': 'CA', 'New York': 'NY', 1200: 'Low Sales', 1500: 'High Sales' }) print("\nDataFrame after multiple replacements:") print(df_replaced) Check out Convert DataFrame To NumPy Array Without Index in Python · The loc[] method in Python allows you to replace values based on conditions, which gives you more flexibility. Here’s an example with sales data categorization: import pandas as pd # Sample US sales data data = { 'Product': ['Laptop', 'Smartphone', 'Tablet', 'Monitor', 'Keyboard'], 'Sales': [1200, 1800, 950
🌐
Python Examples
pythonexamples.org › pandas-dataframe-replace-multiple-values
Pandas DataFrame - Replace Multiple Values
The syntax to replace multiple values in a column of DataFrame is · DataFrame.replace({'column_name' : { old_value_1 : new_value_1, old_value_2 : new_value_2}}) In the following example, we will use replace() method to replace 1 with 11 and 2 with 22 in column a. import pandas as pd df = ...
🌐
CodeForGeek
codeforgeek.com › home › replace multiple values in a dataframe using pandas
Replace Multiple Values in a DataFrame Using Pandas | CodeForGeek
September 30, 2023 - In this article, we’ve explored three unique ways in which we can replace certain row and column values in a DataFrame using – the replace() function with dictionaries, using the loc attribute and using regex patterns for searching substrings.