Reputation: 3685
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
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