WhiteSolstice
WhiteSolstice

Reputation: 651

Python Pandas: sre_constants.error: missing ), unterminated subpattern at position 10

I am trying to iterate a list of strings using dataframe1 to check whether the other dataframe2 has any strings found in dataframe1 to replace them.

for index, row in df.iterrows():
    print( row['x1'] )
    df2['strings'].str.replace(row['x1'],"")

In order to do this I iterated using the code shown above to check and replace for any string found in df1

wait_timeout
interactive_timeout
pool_recycle
....
__all__
folder_name
re.compile('he(lo') <= error string

However, while iterating it attempts to replace the string re.compile('he(lo') but is unable to as the string has "(()" brackets which are uneven. I have read other discussions that replace uses reg expressions and I can fix it by using /(. So I attempted to use:

replace = row['x1'].str.replace("(","\(")
replace = replace.str.replace(")","\)")

But I received an error on replace = replace.str.replace(")","\)") stating that

AttributeError: 'str' object has no attribute 'str'

Upvotes: 0

Views: 2534

Answers (1)

freddiev4
freddiev4

Reputation: 2621

In regards to this error:

AttributeError: 'str' object has no attribute 'str'

You're already assigning a string object to replace, so it doesn't have any attributes called str.

You can fix that with replace = replace.replace(")","\)"), although you might want to consider renaming that replace variable e.g.: some_other_name = replace.replace(")","\)")

Upvotes: 2

Related Questions