If column value contains dictionary key populate other column value with dictionary value

Viewed 348

If I have DataFrame:

df_d = {'Element':['customer full name','full name','name','religion','account number','lgbt','lgbt identity']}
df = pd.DataFrame(data=df_d)
df['Match'} = ''

And I have dictionary:

d = {'name':'Contact', 'religio':'Behavioral', 'lgbt':'Identity'}

How can I populate df['Match'] with the dictionary value if the Element CONTAINS the dictionary key? I can have it populate the column for full matches:

for i in range(len(df)):
     if df['Element'][i] in d:
          df['Match'][i] = d[df['Element'][i]]

But I can't get it to work for partial Element matches. Sorry, my browser won't allow me to copy and paste the cell outputs. Thanks!

1 Answers

Series.str.extract + map

We can construct a regex pattern from the keys of the given mapping dictionary, then use this pattern to extract the capture group in regex pattern then map the capture group with the mapping dictionary

df['Match'] = df['Element'].str.extract(fr"({'|'.join(d.keys())})", expand=False).map(d)

              Element       Match
0  customer full name     Contact
1           full name     Contact
2                name     Contact
3            religion  Behavioral
4      account number         NaN
5                lgbt    Identity
6       lgbt identity    Identity
Related