【问题标题】:Convert a pandas column list of nested dictionaries into separate columns将嵌套字典的 pandas 列列表转换为单独的列
【发布时间】:2019-01-02 21:19:20
【问题描述】:

我有一个这样的 Pandas DataFrame:

Name    Level     Skills

Matt     10       [{'Invisibility': True, 
                    'Powerfist': False, 
                    {'Elemental':[{'Fire': 10,
                                  'Water': 20,
                                  'Wind': 15,
                                  'Earth': 10}],
                     'Race': 'Human'},
                    'Resources': [{'Mana' : True,
                                   'Energy':[{'Solar' : True,
                                              'Air' : True,
                                              'Hydro': True}]}]}]
Jack     12       [{'Invisibility': True, 
                    'Powerfist': False, 
                    {'Elemental':[{'Fire': 5,
                                  'Water': 13,
                                  'Wind': 10,
                                  'Earth': 16}]},
                     'Race': 'Giant'},
                    'Resources': [{'Mana' : True,
                                   'Energy':[{'Solar' : True,
                                              'Air' : False,
                                              'Hydro': True}]}]}]
Zack     20       [{'Invisibility': False, 
                    'Powerfist': True, 
                    {'Elemental':[{'Fire': 10,
                                  'Water': 10,
                                  'Wind': 10,
                                  'Earth': 10}]},
                     'Race': 'Elf'},
                    'Resources': [{'Mana' : True,
                                   'Energy':[{'Solar' : True,
                                              'Air' : True,
                                              'Hydro': True}]}]}]

如您所见,Skills 列中有一个字典列表和嵌套字典列表。

我希望Skills 列中每个字典中的每个 LAST 键都有自己的列。

像这样:

 Name    Level    Invisibility     Powerfist    Fire   Water   Wind Earth   Race     Mana    Solar    Air    Hydro

 Matt     10      True             False        10     20      15     10    Human   True     True     True    True
 Jack     12      True             False         5     13      10     16    Giant   True     True     False    True
 Zack     20      False            True         10     10      10     10    Elf     True     True     True    True

非常感谢任何建议!

【问题讨论】:

  • 你是从 JSON 创建这个数据框的吗?
  • 是的,一个 2 GB 的 JSON 文件 @AdityaK

标签: python json pandas dictionary


【解决方案1】:

我想你在找json_normalize

【讨论】:

    猜你喜欢
    • 2019-05-27
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2022-12-05
    • 2018-10-22
    相关资源
    最近更新 更多