import pandas as pd
df= pd.DataFrame({'Data':['Hey this is 123456 Jonny B Good',
'This is Jonny B Good at 511-233-1137',
'Wow that is Alice N Wonderland A999b',
'Yes hi: Mick E Mouse 1A25629Q88 or ',
'Bye Mick E Mouse A13B ok was seen on '],
'E_ID': ['E11','E11', 'E22', 'E33', 'E33'],
'N_ID' : ['111', '112', '211', '311', '312'],
'Name' : ['JONNY B GOOD', 'JONNY B GOOD',
'ALICE N WONDERLAND',
'MICK E MOUSE', 'MICK E MOUSE'],
})
df
Data E_ID N_ID Name
0 Hey this is 123456 Jonny B Good E11 111 JONNY B GOOD
1 This is Jonny B Good at 511-233-1137 E11 112 JONNY B GOOD
2 Wow that is Alice N Wonderland A999b E22 211 ALICE N WONDERLAND
3 Yes hi: Mick E Mouse 1A25629Q88 or E33 311 MICK E MOUSE
4 Bye Mick E Mouse A13B ok was seen on E33 312 MICK E MOUSE
I have a sample df
as seen above. I also have sample dictionary d
as seen below
d = {'E11': ['Jonny',
'B',
'Good',
'Jonny',
'B',
'Good',
'123456',
'511-233-1137'],
'E22': ['Alice',
'N',
'Wonderland',
'A999b'],
'E33': ['Mick',
'E' ,
'Mouse',
'Mick',
'E' ,
'Mouse',
'1A25629Q88',
'A13B',]}
I would like use the values from d
e.g. Jonny
to change the corresponding value in Data
. So e.g. Jonny
in row 0
will become @@@
.
To do so, I have looked Remap values in pandas column with a dict and how to replace column values with dictionary keys in pandas but they arent much help. I think I need to use something like this
df['New'] = df['Data'].str.replace(d[value], '@@@')
I would like my output to look like this
Data E_ID N_ID Name New
0 Hey this is @@@ @@@ @@@ @@@
1 This is @@@ @@@ @@@ at @@@
2 Wow that is @@@ @@@ @@@ @@@
3 Yes hi: @@@ @@@ @@@ @@@ or
4 Bye @@@ @@@ @@@ @@@ ok was seen on
What do I need to do to get this output?