【发布时间】:2016-09-19 08:45:05
【问题描述】:
我正在使用REST API 来获取json 文件,如下所示:
import urllib2
import pandas as pd
import numpy as np
import requests
request='myrequest'
data= requests.get(request)
json=data.json()
df=pd.DataFrame(json)
数据框看起来像
items
0 {u'access': u'all', u'count': 501, u'time': 2014}
1 {u'access': u'all', u'count': 381, u'time': 2015}
如何在 Pandas 中将这一单列(看起来像字典)转换为适当的列?
编辑
原始 json 数据如下所示
{
"items": [
{
"access": "all",
"count": 200,
"time": 2015
},
{
"access": "all",
"count": 14,
"time": 2015
},
]
}
谢谢!
【问题讨论】:
-
只需将 JSON 加载到字典中,然后再将其转换为 DataFrame。
-
谢谢!我该怎么做?
-
您可以添加您的示例文件吗?我会发布一个答案。
-
你能试试这个吗?
pd.DataFrame(json['items'])(最好不要将该字典命名为“json”,因为它是您可能使用的模块名称。)