I am using a REST API
to get a json
file as follows:
我使用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)
and the dataframe looks like
dataframe是这样的
items
0 {u'access': u'all', u'count': 501, u'time': 2014}
1 {u'access': u'all', u'count': 381, u'time': 2015}
How can I transform this single column (that looks like a dictionary) into proper columns in Pandas?
如何将这一栏(看起来像一本字典)转换成正确的熊猫栏?
EDIT
编辑
the raw json data looks like this
原始json数据如下所示
{
"items": [
{
"access": "all",
"count": 200,
"time": 2015
},
{
"access": "all",
"count": 14,
"time": 2015
},
]
}
Thanks!
谢谢!
2 个解决方案
#1
5
pd.read_json(json_str)
pd.read_json(json_str)
Here is the Pandas documentation.
这是熊猫的文件。
EDIT:
编辑:
For a list of json str you can also just:
对于json str列表,您还可以:
import json
import pandas as pd
df = pd.DataFrame.from_records(map(json.loads, json_lst))
#2
1
Well, it seems to me that JSON import to nesting containing any variations of dicts and list, while Pandas require a single dict collection with iterable elements. You therefore have to do a little bit of conversion if they do not match.
嗯,在我看来,JSON导入到嵌套中,其中包含任何dicts和list的变体,而熊猫需要具有可迭代元素的单个dict集合。因此,如果它们不匹配,就需要进行一些转换。
Assuming I interpret the structure of your JSON correctly (and I might not since, you are only printing the end product, not the JSON structure), it looks like it is a list of dictionaries. If that is the case, here is the solution:
假设我正确地解释了JSON的结构(我可能不会这么做,因为您只是打印最终产品,而不是JSON结构),它看起来像是一个字典列表。如果是这样的话,解决办法是:
data = {k:[v] for k,v in json[0].items()}
for jso in json[1:]:
for k,v in jso.items():
data[k].append(v)
df = pd.DataFrame(data)
Edit:
编辑:
Values are provided, to get my code working, you just need the following in front:
提供了值,为了让我的代码正常工作,您只需要以下内容:
json = json["items"]
I think this should work, but it depends on how requests processes JSON. Give me a printout of the json
object if it doesn't work.
我认为这应该是可行的,但这取决于请求如何处理JSON。如果json对象不能工作,请给我一个打印输出。
#1
5
pd.read_json(json_str)
pd.read_json(json_str)
Here is the Pandas documentation.
这是熊猫的文件。
EDIT:
编辑:
For a list of json str you can also just:
对于json str列表,您还可以:
import json
import pandas as pd
df = pd.DataFrame.from_records(map(json.loads, json_lst))
#2
1
Well, it seems to me that JSON import to nesting containing any variations of dicts and list, while Pandas require a single dict collection with iterable elements. You therefore have to do a little bit of conversion if they do not match.
嗯,在我看来,JSON导入到嵌套中,其中包含任何dicts和list的变体,而熊猫需要具有可迭代元素的单个dict集合。因此,如果它们不匹配,就需要进行一些转换。
Assuming I interpret the structure of your JSON correctly (and I might not since, you are only printing the end product, not the JSON structure), it looks like it is a list of dictionaries. If that is the case, here is the solution:
假设我正确地解释了JSON的结构(我可能不会这么做,因为您只是打印最终产品,而不是JSON结构),它看起来像是一个字典列表。如果是这样的话,解决办法是:
data = {k:[v] for k,v in json[0].items()}
for jso in json[1:]:
for k,v in jso.items():
data[k].append(v)
df = pd.DataFrame(data)
Edit:
编辑:
Values are provided, to get my code working, you just need the following in front:
提供了值,为了让我的代码正常工作,您只需要以下内容:
json = json["items"]
I think this should work, but it depends on how requests processes JSON. Give me a printout of the json
object if it doesn't work.
我认为这应该是可行的,但这取决于请求如何处理JSON。如果json对象不能工作,请给我一个打印输出。