Python pandas to_json索引格式

时间:2017-06-05 22:05:25

标签: python json pandas

我有像这样的pandas数据框

logger.info("x: " + x + ", y: " + y);

我可以使用to_json()生成一个json数组

        date       size       member
0   2016-08-25     25560      Jason 
1   2016-09-08   1051753      Jason
2   2017-01-25      1312      Jason
3   2017-01-25     74971      Peter
4   2017-02-02       220      George
....
100 2017-05-13   1174405       Jason
101 2017-05-28   4016046       Peter

然而,我怎么能得到这样的东西:

with open('members.json', 'w') as f:
    f.write(data.to_json(orient='records', lines=False))

[
{"date":"2016-08-25","size":25560,"member":"Jason"},
{"date":"2016-09-08","size":1051753,"member":"Jason"},
{"date":"2017-01-25","size":1312,"member":"Jason"},
{"date":"2017-01-25","size":74971,"member":"Peter"},
{"date":"2017-02-02","size":220,"member":"George"},
...
{"date":"2017-05-13","size":1174405,"member":"Jason"},
{"date":"2017-05-28","size":4016046,"member":"Peter"}
]

任何帮助,非常感谢。感谢。

1 个答案:

答案 0 :(得分:0)

我不确定是否可以直接进行全部Pandas转换,但您可以一步一步完成。首先,收集与同一成员有关的所有记录,并将每个组转换为JSON:

u = df.groupby('member')\
    .apply(lambda x:x[['date','size']].to_json(orient='records'))

然后,将结果合并到一个JSON数组中:

result = '[' + ','.join(u) + ']'
#'''[
#    [{"date":"2017-02-02","size":220}],
#    [{"date":"2016-08-25","size":25560},{"date":"2016-09-08","size":1051753},
#     {"date":"2017-01-25","size":1312},{"date":"2017-05-13","size":1174405}],
#    [{"date":"2017-01-25","size":74971},{"date":"2017-05-28","size":4016046}]
#]'''