How to replace dataframe column values with dictionary list keys?

Viewed 27

Supose I have a dictionary:

dic = {'1' : ['A', 'B', 'C'], '2' : ['D', 'E'] , '3' : ['F']}

and a data frame

df = pd.DataFrame()

df["ID"] = pd.Series(["A","B","C","D","E","F"])

df["Colour"] = pd.Series(["Blue","Purple","Green","Red","Pink","Black"])

How would I replace values in column df["ID"] with dictionary keys so that I have 1,2,3 in df["ID"] instead of A,B,C, D, E, F?

Thanks

2 Answers

Invert the dictionary and map:

d = {v:k for k,l in dic.items() for v in l}
# {'A': '1', 'B': '1', 'C': '1', 'D': '2', 'E': '2', 'F': '3'}

df['ID'] = df['ID'].map(d)

NB. If you have duplicated values in the lists, the last one seen will take precedence.

Output:

  ID  Colour
0  1    Blue
1  1  Purple
2  1   Green
3  2     Red
4  2    Pink
5  3   Black
Related