Oct-11-2021, 05:22 PM
Dear Python users,
I am trying to replace substrings in a pandas column with the respective exact substring. Imagine that I have the following text:
text = "I loves us. lovess"
and I would like to obtain
text = "I love usa. lovess"
I tried the following code:
I am trying to replace substrings in a pandas column with the respective exact substring. Imagine that I have the following text:
text = "I loves us. lovess"
and I would like to obtain
text = "I love usa. lovess"
I tried the following code:
import pandas as pd text="i loves us. lovess" df=pd.DataFrame([x.split(';') for x in text.split('\n')]) df['text'] = df[0].str.replace(r"loves","love", regex=True) df['text'] = df['text'].str.replace(r"us","usa", regex=True)However, the result from this code is "I love usa. loves". My original text has hundreds of typos, and I will need to replace several words. Any suggestion about how to improve this code would be appreciated. Thank you!