2016-11-27 3 views
2

RESTful API에서 pandas 데이터 프레임으로 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로 구성된 그것에서 간단한 산점도를 구축과 함께 dataframe를 얻는 것입니다.

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을 관리 : 여기

내가 할 것입니다. 하지만 팬더 데이터 프레임으로 디코딩 할 수는 없습니다. 어떻게 작동합니까?

가 (. 내가 팬더 read_json으로 자신을 시도하지만 확실하지 않다, 그것은 현명한 생각입니다 그것은 어쨌든 작동하지 않습니다.).

답변

4

시도 :

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

원하는 열을 얻을

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

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

산포도


설정

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'객체에 'loads'속성이 없습니다. – Rachel

+3

@Rachel'dict'을 사용하여'json'이라는 이름을 마스크 한 것 같습니다. 'del json' 또는'import json'을 실행하고 다시 시도하십시오. –

+1

@Rachel 설정으로 내 소식을 업데이트했습니다. 바라건대 그 모든 혼란을 해결합니다. – piRSquared