Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas groupby and convert to json list

I have a pandas dataframe like the following

idx, f1, f2, f3
1,   a,  a,  b
2,   b,  a,  c
3,   a,  b,  c
.
.
.
87   e,  e,  e

I need to convert the other columns to list of dictionaries based on idx column. so, final result should be:

idx, features
1 ,  [{f1:a, f2:a, f3:b}, {f1:b, f2:a, f3:c}, {f1:a, f2:b, f3:c}]
.
.
.
87,  [{f1: e, f2:e, f3:e}]

Is it possible to do something like this using groupby in pandas?

like image 823
Abhishek Thakur Avatar asked Feb 24 '16 16:02

Abhishek Thakur


1 Answers

You can use groupby by index and then apply to_json:

print df
    f1 f2 f3
idx         
1    a  a  b
1    b  a  c
1    a  b  c
87   e  e  e

print df.groupby(level=0).apply(lambda x: x.to_json(orient='records'))

1     [{"f1":"a","f2":"a","f3":"b"},{"f1":"b","f2":"...
87                       [{"f1":"e","f2":"e","f3":"e"}]
dtype: object

Or if column idx is not index:

print df
   idx f1 f2 f3
0    1  a  a  b
1    1  b  a  c
2    1  a  b  c
3   87  e  e  e

print df.groupby('idx').apply(lambda x: x.to_json(orient='records'))
idx
1     [{"idx":1,"f1":"a","f2":"a","f3":"b"},{"idx":1...
87              [{"idx":87,"f1":"e","f2":"e","f3":"e"}]
dtype: object
like image 154
jezrael Avatar answered Nov 03 '22 18:11

jezrael