1

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 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.