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

How to convert Dataframe into key value pair list for every single raw?

What is the best way to convert following pandas dataframe to a key value pair like below example:

dataframe:

   col1   col2  col3         col4
0    a    tom    90         Good
1    b    jay    75  Not so good
2    c  harsh    35          Bad
3    d    ras    50    impresive

it is possible to get lists like this
desired ouput:

[
 {
   'key':'col1',
   'value':'a'
 }
{
   'key':'col2',
   'value':'tom'
 }
{
   'key':'col3',
   'value':90,
 }
{
   'key':'col4',
   'value':'Good',
 }
]
[
 {
   'key':'col1',
   'value':'b'
 }
{
   'key':'col2',
   'value':'jay'
 }
{
   'key':'col3',
   'value':75,
 }
{
   'key':'col4',
   'value':'Not so good',
 }
]


for every raw

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

Thank you in advance

>Solution :

A more pythonic way:

>>> [[{'key': k, 'value': v} for v in l]
        for _, k, l in zip(*df.to_dict('split').values())]

[[{'key': 'col1', 'value': 'a'},
  {'key': 'col1', 'value': 'tom'},
  {'key': 'col1', 'value': 90},
  {'key': 'col1', 'value': 'Good'}],
 [{'key': 'col2', 'value': 'b'},
  {'key': 'col2', 'value': 'jay'},
  {'key': 'col2', 'value': 75},
  {'key': 'col2', 'value': 'Not so good'}],
 [{'key': 'col3', 'value': 'c'},
  {'key': 'col3', 'value': 'harsh'},
  {'key': 'col3', 'value': 35},
  {'key': 'col3', 'value': 'Bad'}],
 [{'key': 'col4', 'value': 'd'},
  {'key': 'col4', 'value': 'ras'},
  {'key': 'col4', 'value': 50},
  {'key': 'col4', 'value': 'impresive'}]]

Old answer
Use melt reshape your dataframe then group by index and finally export each group to a list of dict:

>>> [df.to_dict('records') for _, df in df.melt(var_name='key', value_name='value', ignore_index=False).groupby(level=0)]

[[{'key': 'col1', 'value': 'a'},
  {'key': 'col2', 'value': 'tom'},
  {'key': 'col3', 'value': 90},
  {'key': 'col4', 'value': 'Good'}],
 [{'key': 'col1', 'value': 'b'},
  {'key': 'col2', 'value': 'jay'},
  {'key': 'col3', 'value': 75},
  {'key': 'col4', 'value': 'Not so good'}],
 [{'key': 'col1', 'value': 'c'},
  {'key': 'col2', 'value': 'harsh'},
  {'key': 'col3', 'value': 35},
  {'key': 'col4', 'value': 'Bad'}],
 [{'key': 'col1', 'value': 'd'},
  {'key': 'col2', 'value': 'ras'},
  {'key': 'col3', 'value': 50},
  {'key': 'col4', 'value': 'impresive'}]]
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