Abhishek Thakur
Abhishek Thakur

Reputation: 17025

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?

Upvotes: 24

Views: 27243

Answers (1)

jezrael
jezrael

Reputation: 863146

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

Upvotes: 27

Related Questions