I have a dataframe containing old and new, i need to replace the old with respective new value of dataframe df to a text, please help me on mapping the correct values from this dataframe
df = pd.DataFrame({'old':['following', 'certain', 'godwil_5708'],
'new':['the below', 'confidently yes', 'godwil']})
text
text = "my text contains of the 33109 following values RT3123SO55 and also with certain numbers godwil_5708 and 323stwe and 8y9yc2 456453"
Expected output:
'my text contains of the 33109 the below values RT3123SO55 and also with confidently yes numbers godwil and 323stwe and 8y9yc2 456453'
CodePudding user response:
You can use the following code:
for a,b in zip(l["old"].values,l["new"].values):
text=text.replace(a,b)
print(text)
Output: my text contains of the 33109 the below values RT3123SO55 and also with confidently yes numbers godwil and 323stwe and 8y9yc2 456453
CodePudding user response:
Convert your dataframe to a dict then replace values from old (keys) to new (values) with re module:
d = df.set_index('old')['new'].to_dict()
t = re.sub(fr"(?P<old>{'|'.join(d)})", lambda x: d[x.group('old')], text)
Output:
>>> t
'my text contains of the 33109 the below values RT3123SO55 and also with confidently yes numbers godwil and 323stwe and 8y9yc2 456453'
