[譯]使用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