1

I'm trying to produce a customer json string from the values grouped by another column.

Consider following data:

>>> df = pd.DataFrame(
    [
                ["c1", "a1", "123"],
                ["c1", "a2", "456"],
                ["c2", "a1", "789"]
        ],
    index=["row1", "row2", "row3"],
    columns=["col1", "col2", "col3"],
)

From the above dataframe, I want to generate rows grouped by col1 where the next column is a json string representing a list of the items from col2, and col3.

For example,

>>> new_df
      key_id  json_string
row1      c1  '{"values":[{"col2":"a1", "col3":"123"}, {"col2":"a2", "col3":"456"}]'
row2      c2  '{"values":[{"col2":"a1", "col3":"789"}]'

I'm new to pandas but it appears that a combination of apply and to_json will achieve what I want. Can someone help me figure this out?

Thanks!

1 Answer 1

2

Use custom lambda function to convert to dictionary all columns without col1, add values key and convert to json:

import json

f = lambda x: json.dumps({"values": x.to_dict(orient='records')})
df = (df.set_index('col1')
        .groupby('col1')
        .apply(f)
        .rename_axis('key_id')
        .reset_index(name='json_string'))
print (df)
  key_id                                        json_string
0     c1  {"values": [{"col2": "a1", "col3": "123"}, {"c...
1     c2        {"values": [{"col2": "a1", "col3": "789"}]}

If use to_json output is different:

f = lambda x: x.to_json(orient='records')
df = (df.set_index('col1')
        .groupby('col1')
        .apply(f)
        .rename_axis('key_id')
        .reset_index(name='json_string'))
print (df)
  key_id                                        json_string
0     c1  [{"col2":"a1","col3":"123"},{"col2":"a2","col3...
1     c2                       [{"col2":"a1","col3":"78
                                 
Sign up to request clarification or add additional context in comments.

1 Comment

In either case you're using apply against the grouping associated with grouping col1. To customize the json you use to_dict and json.dumps to produce a customer string. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.