Home > Back-end >  Add leading \ for all special chars in pandas dataframe [duplicate]
Add leading \ for all special chars in pandas dataframe [duplicate]

Time:09-29

I have a dataframe with multiple columns and multiple rows. In most columns there is a string with a special char ('@',':','_',...). I now want to replace all special chars with ('@',':','_',...) Right now I use

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

for this. However, doing this for all special chars is tedious. Is there an easier way to do this for all special chars?

CodePudding user response:

You might use regex as follows

import pandas as pd
df = pd.DataFrame({'Name':['Special@character:test_']})
df['Name'] = df['Name'].replace(r'([@:_])', r'\\\1', regex=True)
print(df)

output

                         Name
0  Special\@character\:test\_

Explanation: I used capturing group which is denoted by ( and ) to get any of following @:_, then replace it with backslash (note that it needs to be escaped) \\ followed by content of what was found in said group \1

  • Related