Dance Party2
Dance Party2

Reputation: 7546

Pandas Replace All Substrings in DataFrame

Given the following dataframe:

import pandas as pd
d = pd.DataFrame({'A':['^|^^|^','abc'],'B':['def','^|^']})

d
        A    B
0  ^|^^|^  def
1     abc  ^|^

I need to replace all instances of "^|^" with blank spaces (" ").

The desired result is:

        A    B
0           def
1     abc   

I've tried these, but to no avail:

d.replace('^|^',' ') #(replaces nothing)
d.replace('^|^',' ',regex=True) #(only works for the value in column B, second row)

Thanks in advance!

Upvotes: 1

Views: 93

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

you need to escape special RegEx symbols:

import re

In [191]: d.replace(re.escape('^|^'),' ', regex=True)
Out[191]:
     A    B
0       def
1  abc

Upvotes: 4

Related Questions