You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从多层嵌套字典与字典列表创建指定结构的DataFrame

处理嵌套字典生成层级化DataFrame的问题

我通过API调用获取到一个层级不固定的嵌套字典,虽有一定数据处理经验,但对复杂字典操作并不熟悉。需要创建一个DataFrame,满足以下要求:

  • 各层级对应Level0、Level1等列,列值为字典中各层级的@name值;
  • 属性列名为属性的@name值,列值为对应的@value值。

字典片段

dct = {
    '@id': '21',
    '@name': 'Top Level',
    '@hasChildren': 'true',
    'level': [
        {
            '@id': '22',
            '@name': 'Asset1',
            '@hasChildren': 'true',
            'level': [
                {
                    '@id': '530',
                    '@name': 'Sub Asset1-1',
                    'attributes': {
                        'attribute': [
                            {'@attributeId': '1581', '@name': 'Grouping', '@value': 'Tall'},
                            {'@attributeId': '1141', '@name': 'Asset Reporting', '@value': 'Public'},
                            {'@attributeId': '981', '@name': 'Unit', '@value': 'Tons'}
                        ]
                    }
                }
            ]
        },
        {
            '@id': '101',
            '@name': 'Asset2',
            '@hasChildren': 'true',
            'level': [
                {
                    '@id': '123',
                    '@name': 'Sub Asset2-1',
                    '@hasChildren': 'true',
                    'level': [
                        {
                            '@id': '1061',
                            '@name': 'Store 2-1-1',
                            '@hasChildren': 'true',
                            'level': [
                                {
                                    '@id': '205',
                                    '@name': 'Shelf 23',
                                    'attributes': {
                                        'attribute': [
                                            {'@attributeId': '581', '@name': 'Type', '@value': 'Shirt'},
                                            {'@attributeId': '1161', '@name': 'Region', '@value': 'USA'},
                                            {'@attributeId': '1261', '@name': 'Area_Grouping', '@value': 'East Coast'},
                                            {'@attributeId': '1581', '@name': 'Grouping', '@value': 'Short'},
                                            {'@attributeId': '1141', '@name': 'Asset Reporting', '@value': 'Consolidated'},
                                            {'@attributeId': '981', '@name': 'Unit', '@value': 'Feet'}
                                        ]
                                    }
                                },
                                {
                                    '@id': '213',
                                    '@name': 'Shelf 49',
                                    'attributes': {
                                        'attribute': [
                                            {'@attributeId': '581', '@name': 'Type', '@value': 'Pants'},
                                            {'@attributeId': '1161', '@name': 'Region', '@value': 'USA'},
                                            {'@attributeId': '1261', '@name': 'Area_Grouping', '@value': 'West Coast'},
                                            {'@attributeId': '1581', '@name': 'Grouping', '@value': 'Short'},
                                            {'@attributeId': '1141', '@name': 'Asset Reporting', '@value': 'Consolidated'},
                                            {'@attributeId': '981', '@name': 'Unit', '@value': 'Feet'}
                                        ]
                                    }
                                }
                            ]
                        }
                    ],
                    'attributes': {
                        'attribute': {'@attributeId': '841', '@name': 'Consolidated Reporting', '@value': 'Consolidated'}
                    }
                }
            ]
        }
    ]
}

期望生成的DataFrame结构(属性列有所省略)

Level0    |Level1   |Level2         |Level3         |Level4     
|Grouping|Asset Reporting
Top Level |         |               |               |           |        |       
Top Level |Asset1   |Sub Asset1-1   |               |           |Tall    |Public     
Top Level |Asset2   |Sub Asset2-1   |Store 2-1-1    |Shelf 23   |        |Consolidated   
Top Level |Asset2   |Sub Asset2-1   |Store 2-1-1    |Shelf 49   |Short   |Consolidated   

尝试过的方法

  • 参考资料实现了递归遍历字典的函数,但仍无法将结果转换为目标DataFrame;
  • 尝试过将数据存入DataFrame后使用.explode()和pd.Series组合,但认为应先做更多预处理。

内容的提问来源于stack exchange,提问作者cobonthecorn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 02:55:38