DataFrame 到 JSON 给出荒谬的输出
DataFrame to JSON giving absurd output
我有这个代码
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}]
}
如何实现?
提前致谢。
to_json
returns 一个字符串,而不是你想要的字典。您应该改用 to_dict
方法。
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)
这段代码给出了预期的结果。
感谢大家的努力
我有这个代码
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}]
}
如何实现?
提前致谢。
to_json
returns 一个字符串,而不是你想要的字典。您应该改用 to_dict
方法。
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)
这段代码给出了预期的结果。
感谢大家的努力