如果列值包含字典键,则使用字典值填充其他列值



如果我有DataFrame:

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

我有字典:

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

如果元素包含dictionary键,我如何用dictionary值填充df['Match']?我可以让它填充完整匹配的列:

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

但我无法使它适用于部分元素匹配。很抱歉,我的浏览器不允许我复制和粘贴单元格输出。谢谢

Series.str.extract+map

我们可以从给定映射字典的键构建正则表达式模式,然后使用该模式extract正则表达式模式中的捕获组,然后使用映射字典map捕获组

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

最新更新