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

convert nested dictionary to dataframe with keys & values are columns

I have a nested dictionary like this:

{('1', 'Jim'): 'Pass',
 ('2', 'Nik'): 'Fail',
 ('3', 'Anna'): 'Pass',
 ('4', 'Bob'): 'Fail',
 ('5', 'Sam'): 'Pass',
 ('6', 'Rob'): 'Fail'}

I want to convert this to a data frame like this:

DataFrame('ID':[1,2,3,4,5,6], 
'Name':[Jim, Nik, Anna, Bob, Sam, Rob], 
Result: [Pass, Fail, Pass, Fail, Pass, Fail])

Kindly help!

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 :

Create a series from it, reset its index, and rename the columns:

df = pd.Series(your_dict).reset_index().set_axis(['ID', 'Name', 'Result'], axis=1)

Output:

>>> df
  ID  Name Result
0  1   Jim   Pass
1  2   Nik   Fail
2  3  Anna   Pass
3  4   Bob   Fail
4  5   Sam   Pass
5  6   Rob   Fail
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