I have the following dictionary and DataFrame in python:
dicti = {'328392': 1, '657728': 2, '913532': 3, '0153G23': 4, '23932Z': 5}
| color | num_ID | other |
|---|---|---|
| red | 1 | train |
| green | 3 | car |
| black | 5 | car |
I want to create a new number column with the value of the dictionary key, based on its value.
| color | num_ID | other | number |
|---|---|---|---|
| red | 1 | train | 328392 |
| green | 3 | car | 913532 |
| black | 5 | car | 23932Z |
>Solution :
You can use map, but with a reverse dictionary:
df['number'] = df['num_ID'].map({v:k for k,v in dicti.items()})