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.
CodePudding user response:
tr_dict = {'.': '', ' ': '', '(': '_', ')': ''}
df.columns=df.columns.str.translate(df.columns.str.maketranslate(tr_dict))
CodePudding user response:
You could try a dictionary with but since you are replacing some 'special' regex characters you'll need to escape them
df = df.replace({'\\.':'',' ':'','\\(':'_','\\)':''}, regex=True)
