嵌套 JSON 和 Pandas

Nested JSON and Pandas

我有一个嵌套 JSON 文件,如下所示(许多对象中的前两个):

{
"Abaddon the Despoiler": {
    "Abaddon the Despoiler": {
        "model_count": "1",
        "points_value": "220\u2022",
        "movement": "6\"",
        "weapon_skill": "2+",
        "ballistic_skill": "2+",
        "strength": "5",
        "toughness": "5",
        "wounds": "8",
        "attacks": "6",
        "leadership": "10",
        "save": "2+"
    }
},
"Chaos Lord": {
    "Chaos Lord": {
        "model_count": "1",
        "points_value": "80",
        "movement": "6\"",
        "weapon_skill": "2+",
        "ballistic_skill": "2+",
        "strength": "4",
        "toughness": "4",
        "wounds": "5",
        "attacks": "4",
        "leadership": "9",
        "save": "3+"
    }
},

我想将其转换为如下所示的数据框:

unit model_count points_value movement weapon_skill ballistic_skill strength toughness wounds attacks leadership save
Abaddon the Despoiler 1 220\u2022 6" 2+ 2+ 5 5 8 6 10 +2
Chaos Lord 1 80 6" 2+ 2+ 4 4 5 4 9 +3

我认为解决方案是使用 pandas json_normalize()

    #load json object
with open('./archive/chaos-space-marines.json') as f:
    d = json.load(f)

nycphil = pd.json_normalize(d)

nycphil.head(3)

但是,当我执行以下操作时,我得到一个 1 行 x 115 列的数据框,列名如下

Abaddon the Despoiler.Abaddon the Despoiler.model_count

Abaddon the Despoiler.Abaddon the Despoiler.points_value

Abaddon the Despoiler.Abaddon the Despoiler.movement    

Abaddon the Despoiler.Abaddon the Despoiler.weapon_skill

有什么想法吗?

我建议 list-comprehension 读取您的 dict:

d = {'Abaddon the Despoiler': {'Abaddon the Despoiler': {'model_count': '1', 'points_value': '220•', 'movement': '6"', 'weapon_skill': '2+', 'ballistic_skill': '2+', 'strength': '5', 'toughness': '5', 'wounds': '8', 'attacks': '6', 'leadership': '10', 'save': '2+'}}, 
     'Chaos Lord':            {'Chaos Lord':            {'model_count': '1', 'points_value': '80','movement': '6"', 'weapon_skill': '2+', 'ballistic_skill': '2+', 'strength': '4', 'toughness': '4', 'wounds': '5', 'attacks': '4', 'leadership': '9', 'save': '3+'}}}

data = [{'unit': key, **values[key]} for key, values in d.items()]
nycphil = pd.DataFrame(data)