【问题标题】:Importing JSON into Pandas将 JSON 导入 Pandas
【发布时间】:2017-12-12 08:18:37
【问题描述】:

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

我正在尝试将它们加载到数据框中,如下所示:

import pandas as pd

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

我收到以下错误:

ValueError: Invalid file path or buffer object type: <type 'list'>

我已经尝试过记录定向,但仍然无法正常工作。

【问题讨论】:

  • 能否将my_json的内容添加到您的问题中?

标签: python json pandas dataframe


【解决方案1】:

它会引发错误,因为您的变量类型是 list,如错误末尾所述。

实际上,这更容易堆叠到数据帧中,因为您可以直接将列表堆叠到数据帧中。 因此您可以使用:

df = pd.DataFrame(my_json['entities'])

【讨论】:

    【解决方案2】:

    根据官方documenation orient 应该是“记录”

    df = pd.read_json(json.dumps(b_j['entities']) , orient='records')
    

    【讨论】:

      【解决方案3】:

      您使用 my_json['entities'] 的方式使它看起来像是 Python dict

      根据pandas documentationread_json 接受“有效的 JSON 字符串或类似文件”。您可以使用以下内容将dict 转换为 json 字符串:

      import json
      json_str = json.dumps(my_json["entities"])
      

      您所描述的键 "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')
      

      屈服:

                                                    entity
      0  {u'type': u'business', u'uuid': u'199bca3e-baf...
      1  {u'type': u'business', u'uuid': u'054a7650-b36...
      

      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')
      

      屈服:

         size      type                                  uuid
      0   918  business  199bca3e-baf6-11e6-861b-0ad881f403bf
      1   984  business  054a7650-b36a-11e6-a734-122e0737977d
      

      【讨论】:

        【解决方案4】:

        如果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
        

        【讨论】:

        • 这完全适合我!谢谢:)
        【解决方案5】:

        danielcorin 为我指明了正确的方向。我最终不得不这样做:

        pd.read_json(json.dumps(b_j['entities']) , orient='list')
        

        read_json 方法接受一个字符串,所以我转储实体集合并使用它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-11-12
          • 1970-01-01
          • 2018-08-14
          • 2019-10-13
          • 2019-07-27
          • 2017-02-07
          • 2014-04-28
          • 1970-01-01
          相关资源
          最近更新 更多