替换 pandas 数据框中的括号

She*_*hew 4 python-2.7 pandas

我使用了下面的代码

input_table = input_table.replace(to_replace='(', value="")
Run Code Online (Sandbox Code Playgroud)

替换整个数据框中的括号。但令我惊讶的是,它不起作用。可能出了什么问题?

jez*_*ael 5

由于特殊的正则表达式值,需要regex=True用转义符替换子字符串(

input_table = input_table.replace(to_replace='\(', value="", regex=True)
Run Code Online (Sandbox Code Playgroud)

样本

input_table = pd.DataFrame({
    'A': ['(a','a','a','a(dd'],
    'B': ['a','a(dd', 'ss(d','((']
})
print (input_table)
      A     B
0    (a     a
1     a  a(dd
2     a  ss(d
3  a(dd    ((

input_table = input_table.replace(to_replace='\(', value="", regex=True)
print (input_table)
     A    B
0    a    a
1    a  add
2    a  ssd
3  add     
Run Code Online (Sandbox Code Playgroud)