Home > front end >  Replacing strings with special characters in pandas dataframe
Replacing strings with special characters in pandas dataframe

Time:10-26

I'm looking to replace strings inside DataFrame columns, these strings contain special characters.

I tried the following, but nothing changed in the DataFrame:

data = {'col1': ["series ${z_mask0}", "series ${z_mask1}", "series ${z_mask2}"]}

df = pd.DataFrame(data)
print(df)

old_values = ["${z_mask0}", "${z_mask1}", "${z_mask2}"]

new_values = ["${z_00}", "${z_01}", "${z_02}"]

df = df.replace(old_values_sign, new_values_sign, regex=True)
print(df)

The intended output is: ['series ${z_00}', 'series ${z_01}', 'series ${z_02']

CodePudding user response:

You need to escape the $ character using \ in the old_values list:

old_values = ["\${z_mask0}", "\${z_mask1}", "\${z_mask2}"]

The above should be enough. Here is all the code:

old_values = ["\${z_mask0}", "\${z_mask1}", "\${z_mask2}"]
new_values = ["${z_00}", "${z_01}", "${z_02}"]
df = df.replace(old_values, new_values, regex=True)
print(df)

Output:

      col1
0   series ${z_00}
1   series ${z_01}
2   series ${z_02}

CodePudding user response:

Have you tried using raw strings for the old_values? There are some RegEx characters in there that may be interfering with your result ("{", "}", and "$"). Try this instead:

old_values = [r"${z_mask0}", r"${z_mask1}", r"${z_mask2}"]

Note the r before each string

  • Related