0

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']

2 Answers 2

2

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}
Sign up to request clarification or add additional context in comments.

4 Comments

Works great if the list is short as the one I posted, but I have a second case where the old_values is too long for me to add "\" manually. I'm editing my question based on your answer
Well, I think that if your editions are a lot, you should open a new question. :)
Okay I'll revert my question to how it was and open a new question :)
Out of curiosity, why does escaping work where raw strings don't? I was under the impression that they were (more or less) equivalent.
0

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

1 Comment

I did try it, but nothing changed as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.