Python str.replace与Pandas str.replace中的字符串替换

Python str.replace与Pandas str.replace中的字符串替换,python,string,pandas,replace,Python,String,Pandas,Replace,我需要用其他东西替换反斜杠,并编写此代码来测试基本概念。工作正常: test_string = str('19631 location android location you enter an area enable quick action honeywell singl\dzone thermostat environment control and monitoring') print(test_string) test_string = test_string.replace('si

我需要用其他东西替换反斜杠,并编写此代码来测试基本概念。工作正常:

test_string = str('19631 location android location you enter an area enable quick action honeywell singl\dzone thermostat environment control and monitoring')
print(test_string)

test_string = test_string.replace('singl\\dzone ','singl_dbl_zone ')
print(test_string)

19631 location android location you enter an area enable quick action honeywell singl\dzone thermostat environment control and monitoring
19631 location android location you enter an area enable quick action honeywell singl_dbl_zone thermostat environment control and monitoring
然而,我有一个充满这些(重新配置)字符串的df,当我尝试操作df时,它不起作用

raw_corpus.loc[:,'constructed_recipe']=raw_corpus['constructed_recipe'].str.replace('singl\\dzone ','singl_dbl_zone ')
反斜杠仍然存在

323096  you enter an area android location location environment control and monitoring honeywell singl\dzone thermostat enable quick action 

我认为删除反斜杠本身会更容易:

In [165]: df
Out[165]:
  constructed_recipe
0       singl\dzone

In [166]: df['constructed_recipe'] = df['constructed_recipe'].str.replace(r'\\', '')

In [167]: df
Out[167]:
  constructed_recipe
0        singldzone

str.replace
pd.Series.str.replace
之间存在差异。前者接受子字符串替换,后者接受正则表达式模式

使用
str.replace
,您需要传递一个原始字符串


尝试这个想法,但是“字符串模式”和“模式”之间有什么区别呢?@profhoff Egad,这是一个打字错误。。。我猜我当时脑子里有两个念头,输入了同样的东西:DHeh。一切都好!现在学习更多关于正则表达式模式和原始字符串的知识。
df['col'] = df['col'].str.replace(r'\\d', '_dbl_')