我想使用一个json文件返回一个panda Dataframe ,其中每行都列出了所有数据。json文件如下所示。
{
"building_element_group": [
{
"basetype": "facade",
"building_element": [
{
"type": "Unitised",
"functional_unit": "m2",
"quantity": 5.74,
"element": [
{
"id": "13d22d3b-7fc6-4116-93ad-80c139e006dc",
"type": "glazing",
"quantity_unit": "m2",
"quantity": 3.29,
"material": [
{
"type": "glass",
"impact_data_ID": "5726d14e-d36e-417d-afc4-c70793080186",
"quantity_unit": "m2/m2",
"quantity": 1
}
]
},
{
"id": "045d27e6-8397-4672-9f4a-6cbc5fe4e716",
"type": "cladding",
"quantity_unit": "m2",
"quantity": 6.27,
"material": [
{
"type": "terracotta",
"impact_data_ID": "529d8876-6adb-449c-a12a-74c56aaadc4f",
"quantity_unit": "m/m2",
"quantity": 0.04
},
{
"type": "brick",
"impact_data_ID": "e28d29a9-38f8-4684-a6b1-0615ac7f66e5",
"quantity_unit": "m/m2",
"quantity": 0.06
},
{
"type": "GRC",
"impact_data_ID": "5043ffe6-9d2e-448e-83ed-f36f1f5decfc",
"quantity_unit": "m/m2",
"quantity": 0.025
},
{
"type": "Fiber cement",
"impact_data_ID": "53bbd2be-f9ac-4ee7-88f3-34df68ee5187",
"quantity_unit": "m/m2",
"quantity": 0.013
}
]
}
]
}
]
}
]
}
然后,我加载了上面的文件,并执行了以下操作:
test = pd.json_normalize(df['building_element_group'],
record_path= ['building_element', 'element', 'material'],
meta = ['basetype',
['building_element','quantity'],
['building_element','type'],
['building_element','element', 'quantity_unit'],
['building_element','element', 'type']],
errors='ignore', sep='-')
我想做的是,能够显示所有的json数据在每一行,所以所有的嵌套数据。我已经使用 meta来做到这一点,但我必须手动输入在所有的分支,我需要。有没有办法做到这一点,所以我不需要手动这样做?
1条答案
按热度按时间cbjzeqam1#
考虑使用列表/字典解析(包括在每个级别合并字典),然后将结果传递到DataFrame构造函数中: