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.to_dict('records')pandas.pydata.org/pandas-docs/stable/reference/api/…