Pandas DataFrame to List of Dictionaries
I have the following DataFrame:
customer item1 item2 item3 1 apple milk tomato 2 water orange potato 3 juice mango chips
which I want to translate it to list of dictionaries per row
rows = [
{
'customer': 1,
'item1': 'apple',
'item2': 'milk',
'item3': 'tomato'
}, {
'customer': 2,
'item1':
'water',
'item2': 'orange',
'item3': 'potato'
}, {
'customer': 3,
'item1': 'juice',
'item2': 'mango',
'item3': 'chips'
}
]
Solution 1:
Use df.to_dict('records')
-- gives the output without having to transpose externally.
In [2]: df.to_dict('records')
Out[2]:
[{'customer': 1L, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
{'customer': 2L, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
{'customer': 3L, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}]
Solution 2:
Use df.T.to_dict().values()
, like below:
In [1]: df
Out[1]:
customer item1 item2 item3
0 1 apple milk tomato
1 2 water orange potato
2 3 juice mango chips
In [2]: df.T.to_dict().values()
Out[2]:
[{'customer': 1.0, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
{'customer': 2.0, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
{'customer': 3.0, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}]
Solution 3:
As an extension to John Galt's answer -
For the following DataFrame,
customer item1 item2 item3
0 1 apple milk tomato
1 2 water orange potato
2 3 juice mango chips
If you want to get a list of dictionaries including the index values, you can do something like,
df.to_dict('index')
Which outputs a dictionary of dictionaries where keys of the parent dictionary are index values. In this particular case,
{0: {'customer': 1, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
1: {'customer': 2, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
2: {'customer': 3, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}}
Solution 4:
If you are interested in only selecting one column this will work.
df[["item1"]].to_dict("records")
The below will NOT work and produces a TypeError: unsupported type: . I believe this is because it is trying to convert a series to a dict and not a Data Frame to a dict.
df["item1"].to_dict("records")
I had a requirement to only select one column and convert it to a list of dicts with the column name as the key and was stuck on this for a bit so figured I'd share.