Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a column with the keys of a dictionary in python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use map, but with a reverse dictionary:

df['number'] = df['num_ID'].map({v:k for k,v in dicti.items()})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading