Reputation: 36684
I want to replace values in my df
, but only if the values are a complete match, not partial. Here's an example:
import pandas as pd
df = pd.DataFrame({'Name':['Mark', 'Laura', 'Adam', 'Roger', 'Anna'],
'City':['Los Santos', 'Montreal', 'Los', 'Berlin', 'Glasgow']})
print(df)
Name City
0 Mark Los Santos
1 Laura Montreal
2 Adam Los
3 Roger Berlin
4 Anna Glasgow
I want to replace Los by Los Santos but if I do it the intuitive way, it results in this:
df['City'].str.replace('Los', 'Los Santos')
Out[121]:
0 Los Santos Santos
1 Montreal
2 Los Santos
3 Berlin
4 Glasgow
Name: City, dtype: object
Obviously, I don't want Los Santos Santos.
Upvotes: 1
Views: 51
Reputation: 863341
Use Series.replace
, because Series.str.replace
by default replace by substrings:
df['City'] = df['City'].replace('Los', 'Los Santos')
Upvotes: 4
Reputation: 75120
You can also use:
df['City'].str.replace('.*Los$', 'Los Santos')
0 Los Santos
1 Montreal
2 Los Santos
3 Berlin
4 Glasgow
Name: City, dtype: object
Upvotes: 2