Suppose I have the following df,
d = {'col1':['cat','apple','banana','dog','pen']}
df= pd.DataFrame(d)
that gives
col1
0 cat
1 apple
2 banana
3 dog
4 pen
I want to make a dictionary and map it as a new column to my df, such that I get the following output:
col1 col2
0 cat pet
1 apple fruit
2 banana fruit
3 dog pet
4 pen thing
I have made the following dictionary:
dictionary = {
"pet": ['cat','dog'],
"fruit": ['apple','banana'],
"thing": 'pen'}
but not sure how to implement it as above, a tedious way of doing this is making one by one dictionary and then use map as:
di = {"cat": "pet", "dog": "pet", "apple": "fruit", "banana": "fruit", "pen":"thing"}
and
df['col2'] = df['col1'].map(di)
but this is not the most efficient way I suppose. I wonder how one does this task more efficiently?