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

Dataframe to nested dictionary with column names as keys

index    pair       number      amount
0        158_151    0000002732  104

What I want is:

{‘158_151’: {‘number’: 0000002732, ‘amount’: 104} …

Is that possible?

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 are looking to the to_dict method:

df.set_index("pair").to_dict("index")
{'158_151': {'number': '000000002732', 'amount': 104}}

to_dict accept different argument for the output format. Read the doc in order to provide the correct output for your problem.

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