将嵌套词典的列列表转换为单独的列

2024-05-16 22:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个熊猫数据帧:

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

如有任何建议,我们将不胜感激!你知道吗


Tags: falsetrueairfireskillswindmanarace