首页 > 解决方案 > 当我使用 pandas JSON normalize 时,它​​无法对 JSON 文件的一部分中的数据进行规范化;接下来我该怎么办?

问题描述

我正在使用 pandas json normalize 将 json 转换为 pandas 数据框。我已经能够使用反应和药物列规范化大部分 json。但是,我没有运气的结果。数据示例如下。

"unique_aer_id_number": "-USFDACVM-2015-FN-000622",
  "original_receive_date": "19870417",
  "number_of_animals_affected": "14000",
  "primary_reporter": "Other",
  "number_of_animals_treated": "37000",
  "drug": [
    {
      "route": "Oral",
      "brand_name": "MSK",
      "active_ingredients": [
        {
          "dose": {
            "denominator_unit": "Unknown",
            "numerator": "1",
            "numerator_unit": "Unknown",
            "denominator": "1"
          },
          "name": "Monensin"
        }
      ],
      "atc_vet_code": "QP51AH03",
      "manufacturer": {
        "name": "MSK"
      }
    }
  ],
  "health_assessment_prior_to_exposure": {
    "assessed_by": "Veterinarian"
  },
  "onset_date": "19870417",
  "report_id": "N038878",
  "animal": {
    "gender": "Mixed",
    "species": "Chicken",
    "weight": {
      "unit": "Kilogram",
      "min": ".910",
      "max": ".910",
      "qualifier": "Measured"
    },
    "age": {
      "unit": "Week",
      "min": "3.00",
      "max": "3.00",
      "qualifier": "Measured"
    },
    "breed": {
      "is_crossbred": "false",
      "breed_component": "Chicken (unknown)"
    }
  },
  "type_of_information": "Safety Issue",
  "outcome": [
    {
      "medical_status": "Died",
      "number_of_animals_affected": "555"
    }
  ]
},

我正在使用这段代码:

works_out = pd.io.json.json_normalize(data = jsonstr['results'], 
                            record_path ='outcome', meta =['unique_aer_id_number', 'original_receive_date', 'report_id']) 

即使它们是结果值并且我可以规范化其他列,我也会收到此错误消息。

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-28-080471f0f906> in <module>
      1 works_out = pd.io.json.json_normalize(data = jsonstr['results'], 
----> 2                             record_path ='outcome', meta =['unique_aer_id_number', 'original_receive_date', 'report_id']) 

~\Anaconda3\lib\site-packages\pandas\io\json\_normalize.py in json_normalize(data, record_path, meta, meta_prefix, record_prefix, errors, sep, max_level)
    323                 records.extend(recs)
    324 
--> 325     _recursive_extract(data, record_path, {}, level=0)
    326 
    327     result = DataFrame(records)

~\Anaconda3\lib\site-packages\pandas\io\json\_normalize.py in _recursive_extract(data, path, seen_meta, level)
    295         else:
    296             for obj in data:
--> 297                 recs = _pull_field(obj, path[0])
    298                 recs = [
    299                     nested_to_record(r, sep=sep, max_level=max_level)

~\Anaconda3\lib\site-packages\pandas\io\json\_normalize.py in _pull_field(js, spec)
    244                 result = result[field]
    245         else:
--> 246             result = result[spec]
    247 
    248         return result

KeyError: 'outcome'

这是我能够使用 JSON 的药物部分规范化的另一个数据框。

代码。

works_drug = pd.io.json.json_normalize(data = jsonstr['results'], 
                            record_path ='drug', meta =['unique_aer_id_number', 'original_receive_date', 'report_id', 'number_of_animals_affected']) 

结果

在此处输入图像描述

标签: pythonjsonpython-3.xpandasdataframe

解决方案


推荐阅读