Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas: error: missing ), unterminated subpattern at position 2

Tags:

python

pandas

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 <<?

like image 926
Umar Yusuf Avatar asked Jan 02 '17 11:01

Umar Yusuf


2 Answers

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

like image 99
furas Avatar answered Nov 09 '22 15:11

furas


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), '>>')
like image 11
loretoparisi Avatar answered Nov 09 '22 13:11

loretoparisi