Python 如何将json加载到pandas数据帧中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37374568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to load a json into a pandas dataframe?
提问by ??????
I am using a REST API
to get a json
file as follows:
我正在使用 aREST 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
数据框看起来像
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?
如何将这个单列(看起来像字典)转换为 Pandas 中的适当列?
EDIT
编辑
the raw json data looks like this
原始 json 数据如下所示
{
"items": [
{
"access": "all",
"count": 200,
"time": 2015
},
{
"access": "all",
"count": 14,
"time": 2015
},
]
}
Thanks!
谢谢!
采纳答案by Lifu Huang
pd.read_json(json_str)
pd.read_json(json_str)
Here is the Pandas documentation.
这是Pandas 文档。
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))
回答by jonathf
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 的任何变体,而 Pandas 需要具有可迭代元素的单个 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
如果它不起作用,请给我一个对象的打印输出。