Dataframe with below structure -
ID text
0 Language processing in python th is great
1 Relace the string
Dictionary named custom fix
{'Relace': 'Replace', 'th' : 'three'}
Tried the code and the output is coming as - Current output -
ID text
0 Language processing in pythirdon three is great
1 Replace threee string
Code:
def multiple_replace(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
df['col1'] = df.apply(lambda row: multiple_replace(custom_fix, row['text']), axis=1)
Expected Output -
ID text
0 Language processing in python three is great
1 Replace the string