Mor*_*tch 8 python json dataframe pandas
我必须遵循来自API(例如my_json)的JSON。实体数组存储在称为“实体”的键中:
{
"action" : "get",
"application" : "4d97323f-ac0f-11e6-b1d4-0eec2415f3df",
"params" : {
"limit" : [ "2" ]
},
"path" : "/businesses",
"entities" : [
{
"uuid" : "508d56f1-636b-11e7-9928-122e0737977d",
"type" : "business",
"size" : 730 },
{
"uuid" : "2f3bd4dc-636b-11e7-b937-0ad881f403bf",
"type" : "business",
"size" : 730
} ],
"timestamp" : 1499469891059,
"duration" : 244,
"count" : 2
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将它们加载到数据框中,如下所示:
import pandas as pd
pd.read_json(my_json['entities'], orient='split')
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ValueError: Invalid file path or buffer object type: <type 'list'>
Run Code Online (Sandbox Code Playgroud)
我已经尝试过记录定向,但仍然无法正常工作。
如果my_json我怀疑是字典,那么您可以跳过pd.read_json和
pd.DataFrame(my_json['entities'])
size type uuid
0 730 business 508d56f1-636b-11e7-9928-122e0737977d
1 730 business 2f3bd4dc-636b-11e7-b937-0ad881f403bf
Run Code Online (Sandbox Code Playgroud)
您使用的方式my_json['entities']使它看起来像是一个 Python dict。
根据pandas文档,read_json接受“有效的 JSON 字符串或类似文件”。您可以dict使用以下命令将 a转换为 json 字符串:
import json
json_str = json.dumps(my_json["entities"])
Run Code Online (Sandbox Code Playgroud)
"entities"您所描述的键下的数据不符合orient="split". 看起来您将需要使用orient="list":
import pandas as pd
my_json = """{
"entities": [
{
"type": "business",
"uuid": "199bca3e-baf6-11e6-861b-0ad881f403bf",
"size": 918
},
{
"type": "business",
"uuid": "054a7650-b36a-11e6-a734-122e0737977d",
"size": 984
}
]
}"""
print pd.read_json(my_json, orient='list')
Run Code Online (Sandbox Code Playgroud)
产生:
entity
0 {u'type': u'business', u'uuid': u'199bca3e-baf...
1 {u'type': u'business', u'uuid': u'054a7650-b36...
Run Code Online (Sandbox Code Playgroud)
或者
import pandas as pd
my_json = """[
{
"type": "business",
"uuid": "199bca3e-baf6-11e6-861b-0ad881f403bf",
"size": 918
},
{
"type": "business",
"uuid": "054a7650-b36a-11e6-a734-122e0737977d",
"size": 984
}
]"""
print pd.read_json(my_json, orient='list')
Run Code Online (Sandbox Code Playgroud)
产生:
size type uuid
0 918 business 199bca3e-baf6-11e6-861b-0ad881f403bf
1 984 business 054a7650-b36a-11e6-a734-122e0737977d
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11088 次 |
| 最近记录: |