I have a Pandas data frame & I need to convert it to list of dictionaries but when I use df.to_dict()
, i'm not getting what I expected.
Data Frame:
Name Class School
0 Alex 4 SVN
1 Julie 4 MSM
After using df.to_dict()
,
{'Name': {0: 'Alex', 1: 'Julie'}, 'Class': {0: 4, 1: 4}, 'School': {0: 'SVN', 1: 'MSM'}}
But, I need something like below,
[{'Name':'Alex', 'Class': 4, 'School':'SVN'}, {'Name':'Julie', 'Class': 5, 'School':'MSM'}]
How can I do this? What's the efficient way of doing this?
试试这个: df.T.to_dict().values()
而不是df.to_dict()
:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.