将数据框添加到字典列表

我有以下数据帧:

客户项目1项目2项目3
1个苹果牛奶番茄
2水橙土豆
3果汁芒果片

我想把它翻译成每行字典的列表

行=[
{
“客户”:1,
'item1':'apple',
“项目2”:“牛奶”,
“项目3”:“西红柿”
}, {
“客户”:2,
“项目1”:
“水”,
'item2':'orange',
“项目3”:“土豆”
}, {
“客户”:3,
'项目1':'果汁',
'项目2':'芒果',
“项目3”:“芯片”
}
]

使用df.to_dict('records')——在不需要外部转置的情况下给出输出

[2]on

:df.to_dict('记录')
Output[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'}]

发表评论