0

我有这个代码

    eventSummary = {}
    eventSummary['jobClassID'] = data['jobClassID'].split()
    eventSummary['modelID'] =data['modelID'].split()
    eventSummary['startDate'] = pd.to_datetime(start).date().strftime("%Y-%m-%d").split()
    eventSummary['hourlySummary']=merged_df.to_json(orient = 'records')
    dataF = pd.DataFrame(eventSummary)
    dataF.to_json(path_or_buf = '*\merged2.json', orient="records")

这给了我这样的json:

{

"jobClassID":"NorthernRegion|FarNorthDistrict|GeneralDuties|Constable|FIR|Senior|Lead|5",
"modelID":"4",
"startDate":
"2019-01-01",
"hourlySummary":
"[{\"Day\":1,\"Hour\":0,\"Month\":1,\"priority\":3,\"Count\":3,\"Hourly Season\":1.5589041096,\"Weekly Season\":1.9205974843,\"Monthly Season\":2.0497311828,\"Noise Factor\":0.6517894485,\"groupId\":0,\"K (Service)\":0.6955185685,\"Theta (Service)\":10924.8330481261,\"K (Travel)\":0.6819906983,\"Theta (Travel)\":5694.9711557023}]

}

但我希望输出是这样的:{

"jobClassID":["NorthernRegion|FarNorthDistrict|GeneralDuties|Constable|FIR|Senior|Lead|5"],
"modelID":["4"],
"startDate":["2019-01-01"],
"hourlySummary":[
                  {"Day":1,"Hour":0,"Month":1,"priority":3,"Count":3,"Hourly Season":1.5589041096,"Weekly Season":1.9205974843,"Monthly Season":2.0497311828,"Noise Factor":0.6517894485,"groupId":0,"K (Service)":0.6955185685,"Theta (Service)":10924.8330481261,"K (Travel)":0.6819906983,"Theta (Travel)":5694.9711557023}]

}

如何做到这一点?

提前致谢。

4

2 回答 2

0

to_json返回一个字符串,而不是你想要的字典。您应该改用to_dict方法。

于 2020-01-27T10:14:13.377 回答
0
eventSummary['jobClassID'] = [data['jobClassID'].split()]
eventSummary['modelID'] = [data['modelID'].split()]
eventSummary['startDate'] = [pd.to_datetime(start).date().strftime("%Y-%m- 
 %d").split()]
eventSummary['hourlySummary']= [merged_df.to_dict(orient='records')]
dataF = pd.DataFrame(eventSummary)
dataF.to_json(path_or_buf = '*\merged12.json', orient="records", lines = True)

此代码按预期给出结果。

谢谢大家的努力。

于 2020-01-27T10:47:07.673 回答