Python str.replace数据帧中的不平衡括号错误

Python str.replace数据帧中的不平衡括号错误,python,pandas,str-replace,Python,Pandas,Str Replace,我有两个数据帧。下面是数据的外观 查找 更换 我正在当前标题中搜索每个关键字,如果找到,我将在查找数据框中将其替换为相应的关键字长度。下面是我的代码 import pandas as pd df_find = pd.read_csv(input_path_find) df_replace = pd.read_csv(input_path_replace) #replace for i in range(df_replace.shape[0]): df_find.current_ti

我有两个数据帧。下面是数据的外观

查找

更换

我正在
当前标题中搜索每个
关键字
,如果找到,我将在
查找
数据框中将其替换为相应的
关键字长度
。下面是我的代码

import pandas as pd
df_find = pd.read_csv(input_path_find)
df_replace = pd.read_csv(input_path_replace)

#replace
for i in range(df_replace.shape[0]):
    df_find.current_title=df_find.current_title.str.replace(df_replace.keyword.loc[i],df_replace.keywordLength.loc[i],case=False)
然而,当我执行代码时,我得到了下面的错误

error                                     Traceback (most recent call last)
<ipython-input-13-134bbf2a1cb4> in <module>()
      1 for i in range(df_replace.shape[0]):
----> 2     df_find.current_title=df_find.current_title.str.replace(df_replace.keyword.loc[i],df_replace.keywordLength.loc[i],case=False)

c:\python27\lib\site-packages\pandas\core\strings.pyc in replace(self, pat, repl, n, case, flags)
   1504     def replace(self, pat, repl, n=-1, case=True, flags=0):
   1505         result = str_replace(self._data, pat, repl, n=n, case=case,
-> 1506                              flags=flags)
   1507         return self._wrap_result(result)
   1508 

c:\python27\lib\site-packages\pandas\core\strings.pyc in str_replace(arr, pat, repl, n, case, flags)
    326         if not case:
    327             flags |= re.IGNORECASE
--> 328         regex = re.compile(pat, flags=flags)
    329         n = n if n >= 0 else 0
    330 

c:\python27\lib\re.pyc in compile(pattern, flags)
    192 def compile(pattern, flags=0):
    193     "Compile a regular expression pattern, returning a pattern object."
--> 194     return _compile(pattern, flags)
    195 
    196 def purge():

c:\python27\lib\re.pyc in _compile(*key)
    249         p = sre_compile.compile(pattern, flags)
    250     except error, v:
--> 251         raise error, v # invalid expression
    252     if not bypass_cache:
    253         if len(_cache) >= _MAXCACHE:

error: unbalanced parenthesis
错误回溯(最近一次调用上次)
在()
1表示范围内的i(df_replace.shape[0]):
---->2 df_find.current_title=df_find.current_title.str.replace(df_replace.keyword.loc[i],df_replace.keywordLength.loc[i],case=False)
替换中的c:\python27\lib\site packages\pandas\core\strings.pyc(self、pat、repl、n、case、flags)
1504 def更换(self、pat、repl、n=-1、case=True、flags=0):
1505结果=str\u替换(自.\u数据,pat,repl,n=n,case=case,
->1506标志=标志)
1507返回自包装结果(结果)
1508
str_replace中的c:\python27\lib\site packages\pandas\core\strings.pyc(arr、pat、repl、n、case、flags)
326如果不是这种情况:
327标志|=重新忽略案例
-->328 regex=re.compile(pat,flags=flags)
329如果n>=0,则n=n,否则0
330
编译中的c:\python27\lib\re.pyc(模式、标志)
192 def编译(模式,标志=0):
193“编译正则表达式模式,返回模式对象。”
-->194返回编译(模式、标志)
195
196 def purge():
c:\python27\lib\re.pyc in_compile(*键)
249 p=sre_compile.compile(模式、标志)
250除错误外,v:
-->251 raise错误,v#表达式无效
252如果不绕过_缓存:
253如果len(\u cache)>=\u MAXCACHE:
错误:不平衡括号
有什么帮助吗

编辑:
str(df_replace.keywordLength.loc[i])
的值包含任何
(*)+[\
特殊字符时,会出现错误。需要正则表达式作为第一个参数。在将模式字符串传递给
str.replace

import pandas as pd
import re
df_find = pd.read_csv(input_path_find)
df_replace = pd.read_csv(input_path_replace)

#replace
for i in range(df_replace.shape[0]):
        df_find.current_title = df_find.current_title.str.replace(
            re.scape(df_replace.keyword.loc[i]),
            df_replace.keywordLength.loc[i],
            case=False
        )

df_replace.keywordLength.loc[i]
中有什么?它在replace表中。第二列。它有上一列的文本语料库的长度。数字要精确,所以我无法在没有源数据的情况下复制错误,但是,考虑到它是
.str.replace
并且它们是数字,你试过
str吗(df_replace.keywordLength.loc[i])
获取替换值?或
.replace
,不使用
.str
?确定,当str(df_replace.keywordLength.loc[i])的值包含任何
(*)+[\
特殊字符时,出现错误的确切位置