从熊猫数据框中替换括号

时间:2018-06-28 07:25:02

标签: python-2.7 pandas

我使用了以下代码

input_table = input_table.replace(to_replace='(', value="")

替换整个数据框中的括号。但是令我惊讶的是,它没有用。有什么问题吗?

1 个答案:

答案 0 :(得分:3)

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

input_table = input_table.replace(to_replace='\(', value="", regex=True)

示例

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