I have a dataframe which contains the characters ((( I would like to replace. But I get error after doing this:
data = [{'Title': 'set1((("a", "b", "c")))'},
     {'Title': 'set2((("d", "e", "f")))'},
     {'Title': 'set3((("g", "h", "i")))'},
     {'Title': 'set4((("j", "k", "l")))'},
     {'Title': 'set5((("m", "n", "o")))'},
     {'Title': 'set6((("p", "q", "r")))'}]
df = pd.DataFrame(data)
df
# df['Title'] = df['Title'].str.replace('set', 'M') # Works correctly
df['Title'] = df['Title'].str.replace('(((', '>>') # Not working
How do I solve this error in order to to replace ((( by >> and ))) by <<?
replace in pandas lets you use regex and ( has special meaning in regex so use \( 
df['Title'] = df['Title'].str.replace('\(\(\(', '>>')
pandas doc: pandas.Series.str.replace
A more general solution would be to escape the input token using re.escape
import re
inputToken = '((('
df['Title'] = df['Title'].str.replace(re.escape(inputToken), '>>')
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With