Dataframe to nested dictionary with column names as keys

Issue

index    pair       number      amount
0        158_151    0000002732  104

What I want is:

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

Is that possible?

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.

Answered By – webersa

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published