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

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

我正在尝试使用 dataframe1 迭代字符串列表,以检查另一个 dataframe2 是否有在 dataframe1 中找到的任何字符串来替换它们。

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

为了做到这一点,我反复使用上面显示的代码来检查和替换 df1

中找到的任何字符串
wait_timeout
interactive_timeout
pool_recycle
....
__all__
folder_name
re.compile('he(lo') <= error string

但是,在迭代时它会尝试替换字符串 re.compile('he(lo') 但无法替换,因为该字符串具有不均匀的“(()”括号。我读过其他关于替换使用 reg 表达式的讨论,我可以使用 /( 修复它。所以我尝试使用:

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

但我在 replace = replace.str.replace(")","\)") 上收到一条错误消息,指出

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

关于这个错误:

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

您已经将字符串对象分配给 replace,因此它没有任何名为 str 的属性。

您可以使用 replace = replace.replace(")","\)") 修复该问题,但您可能需要考虑重命名该 replace 变量,例如:some_other_name = replace.replace(")","\)")