我有pandas.DataFrame
:
import pandas as pd
import json
df = pd.DataFrame([['2016-04-30T20:02:25.693Z', 'vmPowerOn', 'vmName'],['2016-04-07T22:35:41.145Z','vmPowerOff','hostName']],
columns=['date', 'event', 'object'])
date event object
0 2016-04-30T20:02:25.693Z vmPowerOn vmName
1 2016-04-07T22:35:41.145Z vmPowerOff hostName
我想将该数据帧转换为以下格式:
{
"name":"Alarm/Error",
"data":[
{"date": "2016-04-30T20:02:25.693Z", "details": {"event": "vmPowerOn", "object": "vmName"}},
{"date": "2016-04-07T22:35:41.145Z", "details": {"event": "vmPowerOff", "object": "hostName"}}
]
}
到目前为止,我已经尝试过这个:
df = df.to_dict(orient='records')
j = {"name":"Alarm/Error", "data":df}
json.dumps(j)
'{"name": "Alarm/Error",
"data": [{"date": "2016-04-30T20:02:25.693Z", "event": "vmPowerOn", "object": "vmName"},
{"date": "2016-04-07T22:35:41.145Z", "event": "vmPowerOff", "object": "hostName"}
]
}'
但是,这显然不会将详细列放在自己的字典中。
如何有效地将df日期列和所有其他列拆分为JSON的单独部分?
答案 0 :(得分:3)
使用list and dict comprehension,您可以这样做:
[{'date': x['date'], 'details': {k: v for k, v in x.items() if k != 'date'}}
for x in df.to_dict('records')]
df = pd.DataFrame([['2016-04-30T20:02:25.693Z', 'vmPowerOn', 'vmName'],
['2016-04-07T22:35:41.145Z', 'vmPowerOff', 'hostName']],
columns=['date', 'event', 'object'])
print([{'date': x['date'],
'details': {k: v for k, v in x.items() if k != 'date'}}
for x in df.to_dict('records')])
[{'date': '2016-04-30T20:02:25.693Z', 'details': {'event': 'vmPowerOn', 'object': 'vmName'}},
{'date': '2016-04-07T22:35:41.145Z', 'details': {'event': 'vmPowerOff', 'object': 'hostName'}}
]