我需要用其他东西替换反斜杠,并编写这段代码来测试基本概念。工作良好:
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 发布于 2018-03-19 23:15:25
str.replace和pd.Series.str.replace是有区别的。前者接受子字符串替换,后者接受regex模式。
使用str.replace,您需要传递一个原始字符串。
df['col'] = df['col'].str.replace(r'\\d', '_dbl_')发布于 2018-03-19 23:14:41
我认为移除反斜杠本身会更容易:
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 singldzonehttps://stackoverflow.com/questions/49373835
复制相似问题