GLP
GLP

Reputation: 3685

How to convert dataframe to a list of dictionary

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?

Upvotes: 1

Views: 61

Answers (3)

Red
Red

Reputation: 27577

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'}]

Upvotes: 0

Kostas Letros
Kostas Letros

Reputation: 53

You could use:

df.to_dict('records')

as mentioned in here

Upvotes: 1

Tom
Tom

Reputation: 8800

You can do:

df.to_dict(orient='records')

Source

Upvotes: 1

Related Questions