我必须遵循来自API的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
}

我尝试将它们加载到数据框架中,如下所示:
import pandas as pd

pd.read_json(my_json['entities'], orient='split')

我得到以下错误:
ValueError: Invalid file path or buffer object type: <type 'list'>

我试过记录定向,但还是不行。

最佳答案

如果我怀疑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

关于python - 将JSON导入Pandas,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44980845/

10-16 11:37