'How to replace a list of multiple strings in all the columns of a python data frame? [duplicate]

I consider myself as a beginner to midlevel python programmer. I was surprised that I could not find a simple solution for my question on google. I was trying to replace a list of multiple strings in a python dataframe's all the columns. I did the straight forward way (below) and it works fine.

df.columns=df.columns.str.replace('.', '')
df.columns=df.columns.str.replace(' ','')
df.columns=df.columns.str.replace('(','_')
df.columns=df.columns.str.replace(')','')

I wanted to reduce the number of lines and code everything in one line like how I can use multiple .replace() when I try to replace multiple strings in a sentence. For ex,

txt = 'welcome to metaverse_'
txt.replace('w', 'W').replace('m', 'M').replace('_', '!')

out : 'WelcoMe to Metaverse!'

Is there anyway I can reduce the repetitive code and just use one line to replace multiple strings in all the columns?

Note: I have 10s of columns, and not all the columns have all the strings I want to replace

Thanks in advance.



Solution 1:[1]

You could try a dictionary with replace but since you are replacing some 'special' regex characters you'll need to escape them

df = df.replace({'\\.':'',' ':'','\\(':'_','\\)':''}, regex=True)

Solution 2:[2]

tr_dict = {'.': '', ' ': '', '(': '_', ')': ''}
df.columns=df.columns.str.translate(df.columns.str.maketrans(tr_dict))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2