熊猫df到多个嵌套字典/ json

时间:2019-04-04 14:33:37

标签: python pandas

我在将df转换为3级嵌套字典时遇到麻烦,如果没有丑陋的函数遍历每一行,有没有办法做到这一点?类似于.groupby .apply吗?

输入/ DF:

project,stage,error_code,count
Project_1,stage_1,0,8
Project_1,stage_1,1103,3
Project_1,stage_2,0,4
Project_1,stage_2,1103,2
Project_2,stage_1,0,14
Project_2,stage_1,1103,2
Project_2,stage_1,1105,1
Project_2,stage_2,0,5

所需的输出:

[
    'Project_1': {
        'stage_1': {
            '0': 8,
            '1103': 3
        },
        'stage_2': {
            '0': 14,
            '1103': 2
        }
    },
    'Project_2': {
        'stage_1': {
            '0': 14,
            '1103': 2,
            '1105': 1
        },
        'stage_2': {
            '0': 5,
        }
    }
]

1 个答案:

答案 0 :(得分:1)

您可以使用groupbyunstack

d=df.groupby(['project','stage']).\
        apply(lambda x : dict(zip(x['error_code'],x['count']))).\
           unstack(0).to_dict()
Out[12]: 
{'Project_1': {'stage_1': {0: 8, 1103: 3}, 'stage_2': {0: 4, 1103: 2}},
 'Project_2': {'stage_1': {0: 14, 1103: 2, 1105: 1}, 'stage_2': {0: 5}}}