我如何使用python来放置"数据"这个json响应的组件进入数据框架?

时间:2017-04-11 15:11:21

标签: python json pandas

如何使用python来放置"数据"这个json响应的组件进入了pandas数据帧?

{"data":[{"name":"samplea","val1":null,"val2":"0591","date":"2017-04-11"},{"name":"sampleb","val1":null,"val2":"1111","date":"2017-04-11"}],"result_count":2,"page_size":250,"current_page":1,"total_pages":1,"credits":1}

更新: 使用json_normalize会导致以下错误,

TypeError: string indices must be integers, not str

1 个答案:

答案 0 :(得分:1)

使用json_normalize

d = {"data":[{"name":"samplea","val1":None,"val2":"0591","date":"2017-04-11"},{"name":"sampleb","val1":None,"val2":"1111","date":"2017-04-11"}],"result_count":2,"page_size":250,"current_page":1,"total_pages":1,"credits":1}
from pandas.io.json import json_normalize    
df = json_normalize(d, 'data')
print (df)
         date     name  val1  val2
0  2017-04-11  samplea  None  0591
1  2017-04-11  sampleb  None  1111