Pandas 使用带有值列表的字典替换值

Pandas replace values using dictionary with a list of values

我有以下字典和系列。

product_type_dic = {'Facial Care':['Facial_cleanser', 'Beard_oil'], 'Eye Care':['Around-eye_cream', 'Eyeliner']}

s = pd.Series(['Facial_cleanser', 'Beard_oil', 'Around-eye_cream', 'Eyeliner'])

我的目标是使用系列获取字典的键值。 所以结果将是

'Facial Care'
'Facial Care'
'Eye Care'
'Eye Care'

谢谢

Series.map 与反向 product_type_dic 字典一起使用:

product_type_dic = {
    "Facial Care": ["Facial_cleanser", "Beard_oil"],
    "Eye Care": ["Around-eye_cream", "Eyeliner"],
}


inv_product_type_dic = {vv: k for k, v in product_type_dic.items() for vv in v}
s = pd.Series(["Facial_cleanser", "Beard_oil", "Around-eye_cream", "Eyeliner"])

print(s.map(inv_product_type_dic))

打印:

0    Facial Care
1    Facial Care
2       Eye Care
3       Eye Care
dtype: object