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 dict with lists in values to Pandas dataframe

I have a dictionary with the following keys and values

my_dict={'1':{'name':'one',
              'f_10':[1,10,20,30],
              'f_20':[1,20,40,60]},
        '2':{'name':'two',
              'f_10':[2,12,22,32],
              'f_20':[2,22,42,62]}}

How do I convert it to a Pandas DataFrame that will look like:

name      name      f_10           f_20
1         one       [1,10,20,30]   [1,10,20,60]
2         two       [2,12,22,32]   [2,22,42,62]

The lists need to be considered in one column based on the key, if I try to concat these get converted to separate rows in the data frame.

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 :

I would parse that dictionary to DataFrame and have it transposed. For example,

pd.DataFrame(my_dict).T

Result

    name  f_10              f_20
1   one   [1, 10, 20, 30]   [1, 20, 40, 60]
2   two   [2, 12, 22, 32]   [2, 22, 42, 62]
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