Home > OS >  python replace word not substring
python replace word not substring

Time:10-03

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.

CodePudding user response:

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

CodePudding user response:

I would suggest to use a method like that:

import re

text = 'age engage'
for words in ['age']:
    if re.search(r'\b'   words   r'\b', text):
        print('{0}'.format(words).replace(r"age","âge"))

output:

âge
  • Related