0
votes

I have the following dictionary and Series.

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'])

My goal is to get the key values of the dictionary using the Series. So the result will be

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

Thank you

1

1 Answers

1
votes

Use Series.map with inversed product_type_dic dictionary:

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))

Prints:

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