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}]
本文介绍如何使用pandas库中的DataFrame对象转换为字典。通过调用to_dict方法,并指定orient参数,可以将DataFrame按不同方向转换为字典。orient参数可选值包括'index'、'columns'和'records'等。

5557

被折叠的 条评论
为什么被折叠?



