1

I have a hive table like

col1 col2 
id1  value1
id2  value2
id2  value3

I would like to covert it into a list of dictionary like

[{"col1": "id1", "col2": "value1"}, {"col1": "id2", "col2": "value2"}, {"col1": "id2", "col2": "value3"}]

Can anyone suggest what is the simplest way to achieve this?

petezurich
  • 9,280
  • 9
  • 43
  • 57
GLP
  • 3,441
  • 20
  • 59
  • 91

3 Answers3

1

You can do:

df.to_dict(orient='records')

Source

Tom
  • 8,310
  • 2
  • 16
  • 36
1

You could use:

df.to_dict('records')

as mentioned in here

Kostas Letros
  • 53
  • 1
  • 11
0

For pandas, using df.to_dict('records') is great, but if you'd like to see how we can convert it from a regular dictionary to your expected result:

import pandas as pd

df = pd.DataFrame({'col1': ['id1', 'id2', 'id3'],
                   'col2': ['value1', 'value2', 'value3']})

dct = df.to_dict() # Convert to regular dict
lst = [dict(zip(dct, values)) for values in zip(*[dct[k].values() for k in dct])]

print(lst)

Output:

[{'col1': 'id1', 'col2': 'value1'}, 
 {'col1': 'id2', 'col2': 'value2'}, 
 {'col1': 'id3', 'col2': 'value3'}]
Red
  • 26,798
  • 7
  • 36
  • 58