[译]使用to_dict将pandas.DataFrame转换为Python中的字典列表


pandas.DataFrame.to_json返回的是JSON字符串,不是字典.
可以使用to_dict进行字典转换。
使用orient指定方向。

>>> df
   col1  col2
0     1     3
1     2     4
>>> [df.to_dict(orient='index')]
[{0: {'col1': 1, 'col2': 3}, 1: {'col1': 2, 'col2': 4}}]
>>> df.to_dict(orient='records')
[{'col1': 1, 'col2': 3}, {'col1': 2, 'col2': 4}]

原文来源:https://stackoverflow.com/questions/49027872/convert-pandas-dataframe-to-list-of-dictionaries-in-python


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM