python - 当我使用 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'])
结果
解决方案
推荐阅读
- c++ - 替代在命名空间中不起作用的前向声明
- c# - 使用 VideoLibrary 3.0.1 下载 .mp3
- python - 是否可以更改 PyCharm 显示 OpenCV 文档的方式?
- sql - 如何使用 RETURNING 子句插入两个表?
- css - Flexbox 列表项 - 等高嵌套列表项
- promise - Nodejs - xmlhttprequest 承诺永远不会完成
- c# - 地理空间搜索 - Entity Framework Core / PostgreSQL
- swift - 您可以在 Swift 中加载应用程序之前进行测试设置吗?
- ios - 在 iOS 12 上启动应用程序时无法呈现 Alpha
- pandas - 数据框索引行全为 0