gpt4 book ai didi

python - 将 DataFrame 格式化为面向行的 JSON 对象(电子表格样式数据透视表)

转载 作者:太空宇宙 更新时间:2023-11-03 15:12:11 25 4
gpt4 key购买 nike

我有一个带有 MultiIndex 级别的 pandas DataFrame,如下所示:

enter image description here

现在我尝试将此数据帧转换为面向行的 JSON 对象(或 spreadsheet-style pivot table ?),结果应如下所示:

[{"date":"2016-11-08T23:00:00.000Z", "flag":0, "value":545.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" },  
{"date":"2016-11-09T23:00:00.000Z", "flag":0, "value":545.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" },
{"date":"2016-11-10T23:00:00.000Z", "flag":8, "value":-999.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" },
{"date":"2016-11-11T23:00:00.000Z", "flag":0, "value":680.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" },
{"date":"2016-11-12T23:00:00.000Z", "flag":8, "value":-999.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" }]

我尝试使用以下命令

df.reset_index().to_json(orient='records', date_format='iso')

但是 MultiIndex 结构保持不变。

上面使用的虚拟集可以按如下方式创建:

import pandas as pd
from pandas import Timestamp

input_dict = {('module_A',
'parameter_B',
'location_1',
'flag'): { Timestamp('2016-11-09 05:30:00+0630', tz='Asia/Yangon'): 0,
Timestamp('2016-11-10 05:30:00+0630', tz='Asia/Yangon'): 0,
Timestamp('2016-11-11 05:30:00+0630', tz='Asia/Yangon'): 8,
Timestamp('2016-11-12 05:30:00+0630', tz='Asia/Yangon'): 0,
Timestamp('2016-11-13 05:30:00+0630', tz='Asia/Yangon'): 8},
('module_A',
'parameter_B',
'location_1',
'value'): { Timestamp('2016-11-09 05:30:00+0630', tz='Asia/Yangon'): 545.0,
Timestamp('2016-11-10 05:30:00+0630', tz='Asia/Yangon'): 545.0,
Timestamp('2016-11-11 05:30:00+0630', tz='Asia/Yangon'): -999.0,
Timestamp('2016-11-12 05:30:00+0630', tz='Asia/Yangon'): 680.0,
Timestamp('2016-11-13 05:30:00+0630', tz='Asia/Yangon'): -999.0}}

df = pd.DataFrame(input_dict)

如有任何建议,我们将不胜感激。

最佳答案

您需要stack + rename_axis第一:

df = df.stack([0,1,2]).rename_axis(['date','moduleId','parameterId','locationId'])
d = df.reset_index().to_json(orient='records', date_format='iso')
print (d)

[{"date":"2016-11-08T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":0,"value":545.0},
{"date":"2016-11-09T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":0,"value":545.0},
{"date":"2016-11-10T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":8,"value":-999.0},
{"date":"2016-11-11T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":0,"value":680.0},
{"date":"2016-11-12T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":8,"value":-999.0}]

关于python - 将 DataFrame 格式化为面向行的 JSON 对象(电子表格样式数据透视表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44136416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com