如何将 JSON 解码为 pandas 数据帧?

How to decode a JSON into a pandas dataframe?

我正在尝试将从 RESTful API 获得的 JSON 解码为 pandas 数据帧。下面是 JSON 的片段:

{
   "success":true,
   "data":[
      {
         "id":26,
         "name":"A",
         "comment":"",
         "start_time_plan":null,
         "start_time_actual":"2016-09-13 00:00:00",
         "start_time_delta":null,
         "start_time_score":null,
         "start_time_score_achievement":null,
         "start_time_traffic_light":null,
         "end_time_plan":null,
         "end_time_actual":"2016-09-13 00:00:00",
         "end_time_delta":null,
         "end_time_score":null,
         "end_time_score_achievement":null,
         "end_time_traffic_light":null,
         "status":0,
         "measure_schedule_revision_id":63,
         "responsible_user_id":3,
         "created_time":"2016-09-13 11:29:14",
         "created_user_id":3,
         "modified_time":"2016-09-21 16:33:41",
         "modified_user_id":3,
         "model":"Activity"
      },
      {
         "id":27,
         "name":"B",
         "comment":"",
         "start_time_plan":null,
         "start_time_actual":"2016-09-13 00:00:00",
         "start_time_delta":null,
         "start_time_score":null,
         "start_time_score_achievement":null,
         "start_time_traffic_light":null,
         "end_time_plan":null,
         "end_time_actual":"2016-09-13 00:00:00",
         "end_time_delta":null,
         "end_time_score":null,
         "end_time_score_achievement":null,
         "end_time_traffic_light":null,
         "status":0,
         "measure_schedule_revision_id":63,
         "responsible_user_id":3,
         "created_time":"2016-09-13 11:29:48",
         "created_user_id":3,
         "modified_time":"2016-10-16 18:14:36",
         "modified_user_id":1,
         "model":"Activity"
      }
   ]
}

我的目标是获得一个包含两列的数据框,其中包含 end_time_deltastart_time_delta,并从中构建一个简单的散点图。

我是这样做的:

u = 'https://myurl.com'

# urllib3 + poolmanager for requests
import urllib3
http = urllib3.PoolManager()


# get the JSON
import json
r = http.request('GET', u)
result = json.loads(r.data.decode('utf-8'))

for item in result['data']:
    print(item['end_time_delta'])

for item in result['data']:
    print(item['start_time_delta'])

# decode JSON to pandas dataframe
import pandas as pd

df = pd.read_json( result, orient='values')

我设法得到 JSON 并 forloop 通过它。但我无法将其解码为 pandas 数据帧。它是如何工作的?

(我用 pandas read_json 试了一下。但我不确定,这是个聪明的主意。无论如何它都不起作用。)

尝试:

df = pd.DataFrame(json.loads(j)['data'])

获取您想要的列

df[['start_time_delta', 'end_time_delta']]

散点图

df[['start_time_delta', 'end_time_delta']].plot.scatter(0, 1)

设置

import json

j = """{
   "success":true,
   "data":[
      {
         "id":26,
         "name":"A",
         "comment":"",
         "start_time_plan":null,
         "start_time_actual":"2016-09-13 00:00:00",
         "start_time_delta":null,
         "start_time_score":null,
         "start_time_score_achievement":null,
         "start_time_traffic_light":null,
         "end_time_plan":null,
         "end_time_actual":"2016-09-13 00:00:00",
         "end_time_delta":null,
         "end_time_score":null,
         "end_time_score_achievement":null,
         "end_time_traffic_light":null,
         "status":0,
         "measure_schedule_revision_id":63,
         "responsible_user_id":3,
         "created_time":"2016-09-13 11:29:14",
         "created_user_id":3,
         "modified_time":"2016-09-21 16:33:41",
         "modified_user_id":3,
         "model":"Activity"
      },
      {
         "id":27,
         "name":"B",
         "comment":"",
         "start_time_plan":null,
         "start_time_actual":"2016-09-13 00:00:00",
         "start_time_delta":null,
         "start_time_score":null,
         "start_time_score_achievement":null,
         "start_time_traffic_light":null,
         "end_time_plan":null,
         "end_time_actual":"2016-09-13 00:00:00",
         "end_time_delta":null,
         "end_time_score":null,
         "end_time_score_achievement":null,
         "end_time_traffic_light":null,
         "status":0,
         "measure_schedule_revision_id":63,
         "responsible_user_id":3,
         "created_time":"2016-09-13 11:29:48",
         "created_user_id":3,
         "modified_time":"2016-10-16 18:14:36",
         "modified_user_id":1,
         "model":"Activity"
      }
   ]
}"""