2016-11-27 43 views
2

我想將從RESTful API獲取的JSON解碼爲熊貓數據框。這裏的JSON的樣子片段:如何將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,並通過它for循環。但我無法將其解碼爲熊貓數據框。這是如何運作的?

(我想自己離開與熊貓read_json但我不知道,這是一個聰明的主意它不反正工作。)

回答

4

嘗試:

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" 
     } 
    ] 
}""" 
+0

感謝您的快速回復!我可以看到如何工作。但是我得到以下錯誤:'AttributeError:'dict'object has no attribute'loads'' – Rachel

+3

@Rachel它似乎用'dict'掩蓋了名字'json';做'del json'或'import json'並再次嘗試。 –

+1

@Rachel我已經使用設置更新了我的帖子。希望能夠消除任何混亂。 – piRSquared