Adrian Keister
Adrian Keister

Reputation: 1025

Python pandas replace function not working with escaped characters

I've already looked at half a dozen SO questions on the Python 3 pandas replace function, and none of them apply to this situation. I have the text \" present in some data, and I need to eliminate ONLY the backslash. Toy code:

import pandas as pd
df = pd.DataFrame(columns=['a'])
df.loc[0] = ['Replace \\"']
df

with output

            a
0  Replace \"

My goal is to rewrite df so that it looks like this:

           a
0  Replace "

None of the following work:

df.replace('\\"', '"', regex=True)
df.replace('\\"', '\"', regex=True)
df.replace('\\\"', '\"', regex=True)
df.replace('\\\"', '\"', regex=True)
df.replace(r'\"', r'"', regex=True)
df.replace({'\\"':'"'}, regex=True)
df.replace({r'\"':r'"'}, regex=True)
df.replace(to_replace=r'\"', value=r'"', regex=True)
df.replace(to_replace=r'\"', value=r'"', regex=False)

I can't search just for the backslash, because I have legitimate backslashes elsewhere in the data that I don't want to replace.

Thanks for your time!

Upvotes: 2

Views: 629

Answers (2)

faceless_programmer
faceless_programmer

Reputation: 1

Try

df.a.str.replace('\\','')

result:

0    Replace "

For the whole data frame you can use:

for col in df:
    df[col] = df[col].str.replace(r'\\','')

Upvotes: 0

Mayank Porwal
Mayank Porwal

Reputation: 34056

You can use apply:

In [2596]: df.apply(lambda x: x.str.replace(r'\\"', r'"')) 
Out[2596]: 
           a
0  Replace "

If there's only column in question, you can also do this, which will be a little more performant:

In [2614]: df['a'].str.replace(r'\\"', r'"')
Out[2614]: 
0    Replace "
Name: a, dtype: object

Upvotes: 2

Related Questions