Skip to content Skip to sidebar Skip to footer

How To Replace Multiple Substrings In A Pandas Series Using A Dictionary?

I have a Pandas series of strings. I want to make multiple replacements to multiple substrings per row, see: testdf = pd.Series([ 'Mary went to school today', 'John went to

Solution 1:

You can use:

#Borrowed from an external website
def multipleReplace(text, wordDict):
    forkeyin wordDict:
        text = text.replace(key, wordDict[key])
    returntext

print(testdf.apply(lambda x: multipleReplace(x,to_sub)))

0    Alice went to hospital yesterday
1     John went to hospital yesterday

EDIT

Using the dictionary as below mentioned comments:

to_sub = {
'Mary': 'Alice',
'school': 'hospital',
'today': 'yesterday',
'tal': 'zzz'
}

testdf.apply(lambda x: ' '.join([to_sub.get(i, i) for i in x.split()]))

Outputs:

0    Alice went to hospital yesterday
1     John went to hospital yesterday

Solution 2:

It is working for me in panadas 23.0 version...

Given DataFrame:

>>> testdf
0      Mary went to school today
1    John went to hospital today
dtype:object

Values Which needs to be replaced.

>>>replace_values = {'Mary': 'Alice', 'school': 'hospital', 'today': 'yesterday'}

Resulted Outcome:

>>> testdf.replace(replace_values, regex=True)
0    Alice went to hospital yesterday
1     John went to hospital yesterday
dtype:object

Another example With Desired Result :

Including Partial string ('tal': 'zzz') manulation with replace ..

>>>replace_values = {'Mary': 'Alice', 'school': 'hospital', 'today': 'yesterday', 'tal': 'zzz'}>>>testdf.replace(replace_values, regex=True)
0    Alice went to hospizzz yesterday
1     John went to hospizzz yesterday
dtype: object

Post a Comment for "How To Replace Multiple Substrings In A Pandas Series Using A Dictionary?"