简体   繁体   中英

Python pandas to_json format of indexes

I have a pandas dataframe like this

        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

And I can use to_json() to produce a json array

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"}
]

However, how could I end up with something like this:

[ // Jason
 [ 
  {"date":"2016-08-25","size":25560},
  {"date":"2016-09-08","size":1051753},
  {"date":"2017-01-25","size":1312},
   ...
  {"data":"2017-05-13", "size":1174405}
 ],

 [ // Peter
  {"date":"2017-01-25","size":74971},
  {"date":"2017-05-28","size":4016046}
 ],

 [ // George
  {"date":"2017-02-02","size":220}
 ],
   ...
]

Any help, is very much appreciated. Thanks.

I am not sure if direct all-Pandas conversion is possible, but you can do it step by step. First, collect all records pertaining to the same member, and convert each group to JSON:

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

Then, combine the results into a JSON array:

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}]
#]'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM