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:
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!
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:
1 2 3 4 5 |
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 ) |