Search code examples
pythonstringdataframereplacepython-re

python replace word not substring


I want to replace a word by another in a column of a dataframe. Here is my python code:

import pandas as pd
text="age engage"
df=pd.DataFrame([x.split(';') for x in text.split('\n')])
df['text'] = df[0].str.replace(r"age","âge")

The codes that I found on stackoverflow (and this one included) permit me to obtain df['text']="âge engâge", when I want to obtain df['text']="âge engage". Could anyone help me improve my code? I have dozens of different words that I want to replace in my dataframe, so I would like to use a simple code to implement to different words.


Solution

  • Try-

    import pandas as pd
    text="age engage"
    df=pd.DataFrame([x.split(';') for x in text.split('\n')])
    df['text'] = df[0].str.replace(r"^age","âge") # the carrot indicates to only match from the start of the line
    

    Result

            0        text
    0  age engage  âge engage 
    

    You can also look in regex, a good site to play around when it comes to pattern matching Regex 101