Wrap your keyword with the word boundary character \b:

df['Text'].str.replace(r'\beng\b', 'engine')

0           engine is here
1     engine needs washing
2    engine is overheating
Name: Text, dtype: object

If you have multiple keywords to replace in this manner, pass a dictionary to replace with the regex=True switch:

repl = {'eng' : 'engine'}
repl = {rf'\b{k}\b': v for k, v in repl.items()}

df['Text'].replace(repl, regex=True)

0           engine is here
1     engine needs washing
2    engine is overheating
Name: Text, dtype: object
Answer from cs95 on Stack Overflow
Top answer
1 of 2
2

Using re, in case you want to specify the series string:

df.apply(lambda x: re.sub('\s*{}$'.format(x['series']), '', x['id']), axis=1)

In case the the series string is always a predictable pattern (i.e. [a-z]) you can also try:

df['id'].apply(lambda x: re.sub('\s*[a-z]+$', '', x))

Either way the output is what you are looking for:

0        abarth 1.4
1          abarth 1
2    land rover 1.3
3      land rover 2
4      land rover 5
5        mazda 4.55
2 of 2
1

You could use str.rpartition to split the ids on the last space.

In [169]: parts = df['id'].str.rpartition(' ')[[0,2]]; parts
Out[169]: 
                0   2
0      abarth 1.4   a
1        abarth 1   a
2  land rover 1.3   r
3      land rover   2
4    land rover 5   g
5      mazda 4.55  bl

Then you could use == to compare parts[2] to df['series']:

In [170]: mask = (parts[2] == df['series']); mask
Out[170]: 
0     True
1     True
2     True
3    False
4     True
5     True
dtype: bool

And finally, use df['id'].where to replace df['id] with parts[0] where mask is True:

import pandas as pd
df = pd.DataFrame(
    {'id' : ['abarth 1.4 a','abarth 1 a','land rover 1.3 r','land rover 2',
             'land rover 5 g','mazda 4.55 bl'], 
     'series': ['a','a','r','','g', 'bl'] })
parts = df['id'].str.rpartition(' ')[[0,2]]
mask = (parts[2] == df['series'])
df['id'] = df['id'].where(~mask, parts[0], axis=0)
print(df)

yields

               id series
0      abarth 1.4      a
1        abarth 1      a
2  land rover 1.3      r
3    land rover 2       
4    land rover 5      g
5      mazda 4.55     bl

Alternatively, you could use

import re
def remove_series(x):
    pat = r'{}$'.format(x['series'])
    return re.sub(pat, '', x['id'])
df['id'] = df.apply(remove_series, axis=1)

But calling df.apply with a custom function tends to be much slower than using built-in vectorize methods such as those used in the first method.

🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.replace.html
pandas.DataFrame.replace — pandas 3.0.6 documentation
Replace values based on boolean condition. ... Apply a function to a Dataframe elementwise. ... Map values of Series according to an input mapping or function. ... Simple string replacement.
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › python-pandas-series-str-replace-to-replace-text-in-a-series
Python | Pandas Series.str.replace() to replace text in a series - GeeksforGeeks
July 11, 2025 - Example 1: Replacing values in age column In this example, all the values in age column having value 25.0 are replaced with "Twenty five" using str.replace() After that, a filter is created and passed in .where() method to only display the rows which have Age = "Twenty five".
🌐
Python Forum
python-forum.io › thread-35236.html
replace exact word
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 follow...
🌐
w3resource
w3resource.com › pandas › series › series-str-replace.php
Pandas Series: str.replace() function - w3resource
May 20, 2026 - import numpy as np import pandas as pd pd.Series(['full', 'fog', np.nan]).str.replace('f', repr) Output: 0 <re.Match object; span=(0, 1), match='f'>ull 1 <re.Match object; span=(0, 1), match='f'>og 2 NaN dtype: object · Example - Reverse every lowercase alphabetic word: Python-Pandas Code: import numpy as np import pandas as pd repl = lambda m: m.group(0)[::-1] pd.Series(['full 234', 'brr bzz', np.nan]).str.replace(r'[a-z]+', repl) Output: 0 lluf 234 1 rrb zzb 2 NaN dtype: object ·
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › replace-characters-in-strings-in-pandas-dataframe
Replace Characters in Strings in Pandas DataFrame - GeeksforGeeks
July 23, 2025 - Example 1: The following program is to replace a character in strings for the entire dataframe. ... # import pandas import pandas as pd data = {'Student_Full_Name': ['Mukul_Jatav', 'Rahul_Shukla', 'Robin_Singh', 'Mayank_Sharma', 'Akash_Verma'], 'Father_Full_name': ['Mukesh_Jatav', 'Siddhart_Shukla', 'Rohit_Singh', 'Sunil_Sharma', 'Rajesh_Verma'] } # create an dataframe df = pd.DataFrame(data, columns=['Student_Full_Name', 'Father_Full_name']) # print dataframe print(" original dataframe \n", df) # replace '_' with '-' df = df.replace('_', '+', regex=True) # print dataframe print(" After replace character \n", df)
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas replace substring in dataframe
Pandas Replace Substring in DataFrame - Spark By {Examples}
June 6, 2025 - You can find how to replace substrings in a pandas DataFrame column using the replace() method with lambda functions. This versatile method allows you to
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › how to replace string in pandas dataframe
How to Replace String in Pandas DataFrame - Spark By {Examples}
June 13, 2025 - In pandas, to replace a string in the DataFrame column, you can use either the replace() function or the str.replace() method along with lambda methods.
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › Series › str › replace
Python Pandas Series str replace() - Replace Substring | Vultr Docs
November 26, 2024 - Use the replace() method to target and replace specific substrings. ... import pandas as pd data = pd.Series(['foo', 'bar', 'baz', 'foobar']) modified_data = data.str.replace('foo', 'new') print(modified_data) Explain Code
🌐
Towards Data Science
towardsdatascience.com › home › latest › 2 different replace functions of python pandas
2 Different Replace Functions of Python Pandas | Towards Data Science
January 20, 2025 - The "str.replace" can be used for replacing entire strings but make sure the string to be replaced is not a substring in another value. Let's do an example to demonstrate this case.
🌐
Stack Overflow
stackoverflow.com › questions › 28986489 › how-to-replace-text-in-a-string-column-of-a-pandas-dataframe
python - How to replace text in a string column of a Pandas dataframe? - Stack Overflow
I have a column in my dataframe like this: range "(2,30)" "(50,290)" "(400,1000)" ... and I want to replace the , comma with - dash. I'm currently using this method ...
🌐
Statology
statology.org › home › how to use str.replace in pandas (with examples)
How to Use str.replace in Pandas (With Examples)
April 11, 2024 - Often you may want to replace each occurrence of a particular pattern or substring in a pandas Series. The easiest way to do so is by using the str.replace() function, which uses the following basic syntax:
🌐
Programiz
programiz.com › python-programming › pandas › methods › series-str-replace
Pandas str.replace() (With Examples)
The str.replace() method in Pandas is used to replace a substring within each string element of a Series with another string.
🌐
Stack Overflow
stackoverflow.com › questions › 26154516 › pandas-dataframe-replace-full-word
pandas dataframe replace full word - Stack Overflow
June 4, 2017 - import pandas as pd x = pd.DataFrame(["interesting sting", "an answer", "red and redundant"]) rep = ["sting", "an", "red"] val = ["string", "no", "blue"] x.replace(rep,val,inplace=True, regex=True) print x ... There are several unwanted replacements. I could loop through the list and do a word replace, but I have a very large dataset and looping may not be efficient. I'm also aware of using '\b' for whole word replacement.