为每个唯一列使用嵌套的json将csv转换为json

时间:2018-10-29 06:39:01

标签: python json pandas

我有一个这样的csv文件:

a   b   c   d   e   f
1   3   6   11  16  21
1   4   7   12  16  21
2   3   8   13  18  23
2   4   9   14  18  23
2   5   10  15  18  23

我想从此csv生成json,而json看起来像

{
 {"a":1,
 "data":[{"b":3,"c":6,"d":11},{"b":4,"c":7,"d":12}],
 "e":16,
 "f":21},
{"a":2,
 "data":[{"b":3,"c":8,"d":13},{"b":4,"c":9,"d":14}, 
 {"b":5,"c":10,"d":15}],
 "e":18,
 "f":23}
}

在这里,e和f对于每个a都是固定的,只有b,c,d改变。如何使用python做到这一点。

1 个答案:

答案 0 :(得分:1)

groupbyapplyto_dict一起用于嵌套字典,然后由to_json转换为json:

j = (df.groupby(['a','e','f'])['b','c','d']
       .apply(lambda x: x.to_dict('r'))
       .reset_index(name='data')
       .to_json(orient='records')
       )

print (j)

[{
    "a": 1,
    "e": 16,
    "f": 21,
    "data": [{
        "b": 3,
        "c": 6,
        "d": 11
    }, {
        "b": 4,
        "c": 7,
        "d": 12
    }]
}, {
    "a": 2,
    "e": 18,
    "f": 23,
    "data": [{
        "b": 3,
        "c": 8,
        "d": 13
    }, {
        "b": 4,
        "c": 9,
        "d": 14
    }, {
        "b": 5,
        "c": 10,
        "d": 15
    }]
}]