【发布时间】:2023-03-21 09:13:01
【问题描述】:
以下代码
import pandas as pd
dic = {'_id': '5436e3abbae478396759f0cf', 'meta': {'clinical': {'benign_malignant': 'benign', 'age_approx': 55, 'sex': 'female', 'diagnosis': 'nevus', 'diagnosis_confirm_type': None, 'anatom_site_general': 'anterior torso', 'melanocytic': True}, 'acquisition': {'image_type': 'dermoscopic', 'pixelsX': 1022, 'pixelsY': 767}}, 'name': 'ISIC_0000000'}
frame = pd.io.json.json_normalize(dic)
抛出一个
KeyError: 'diagnosis_confirm_type'
我使用的是熊猫版0.23.0。该代码在版本0.22.0 中可以正常工作。
更新:
显然,0.23.0 中确实存在导致此问题的错误。见https://github.com/pandas-dev/pandas/pull/21164
【问题讨论】:
-
听起来像 re.sub()。尽管使用 json 库( json.dump() / json.dumps() )也可能有所帮助。
-
好的,如果从文件中加载,文件确实包含有效的 json 字符串。 json 模块成功地将其转换为 Python
dict对象,其中所有键都是字符串,值是字符串、整数、布尔值、无或其他dict。如果你想把它作为一个json字符串打印回来,首先将它转换成json:print(json.dumps(jsonObject)) -
即使您将打印的对象输入到 pd.io.json.json_normalize(object) 中也不行?
-
查看 pandas 版本的更新问题
-
宾果游戏,现在我可以重现它了 :)。在 0.22.0 和 0.23.0 之间发生了一些变化。我必须非常感谢您花时间将其转为minimal reproducible example 和明确的问题陈述。
标签: python json pandas normalization