Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to generate a json file with a nested dictionary from pandas df?

I need to generate a json file with a specific format from a pandas dataframe. The dataframe looks like this:

user_id product_id date
1 23 01-01-2022
1 24 05-01-2022
2 56 05-06-2022
3 23 02-07-2022
3 24 01-02-2022
3 56 02-01-2022

And the json file needs to have the following format:

{
  "user_id": 1,
  "items": [{
        "product_id": 23,
        "date": 01-01-2022
        }, {
        "product_id": 24,
        "date": 05-01-2022
        }]
}
{
 "userid": 2,
 "items": [{
        "product_id": 56,
        "date": 05-06-2022
        }]
}
...etc

I’ve tried the following, but it’s not the right format:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

result = (now.groupby('user_id')['product_id','date'].apply(lambda x: dict(x.values)).to_json())

Any help would be much appreciated!

>Solution :

out = (df[['product_id','date']].apply(dict, axis=1)
       .groupby(df['user_id']).apply(list)
       .to_frame('items').reset_index()
       .to_dict('records'))
print(out)

[{'user_id': 1, 'items': [{'product_id': 23, 'date': '01-01-2022'}, {'product_id': 24, 'date': '05-01-2022'}]},
{'user_id': 2, 'items': [{'product_id': 56, 'date': '05-06-2022'}]}, 
{'user_id': 3, 'items': [{'product_id': 23, 'date': '02-07-2022'}, {'product_id': 24, 'date': '01-02-2022'}, {'product_id': 56, 'date': '02-01-2022'}]}]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading