Avoid pandas str.replace using a regex
·
Answer a question
I have the following pandas dataframe. Say it has two columns: id and search_term:
id search_term
37651 inline switch
I do:
train['search_term'] = train['search_term'].str.replace("in."," in. ")
expecting that the dataset above is unaffected, but I get in return for this dataset:
id search_term
37651 in. in. switch
which means inl is replaced by in. and ine is replaced by in., as if I where using a regular expression, where dot means any character.
How do I rewrite the first command so that, literally, in. is replaced by in. but any in not followed by a dot is untouched, as in:
a = 'inline switch'
a = a.replace('in.','in. ')
a
>>> 'inline switch'
Answers
Try escaping the .:
import pandas as pd
df = pd.DataFrame({'search_term': ['inline switch', 'in.here']})
>>> df.search_term.str.replace('in\\.', 'in. ')
0 inline switch
1 in. here
Name: search_term, dtype: object
更多推荐

所有评论(0)