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

Convert Pivot Like Data into JSON using Python or Pandas

I am a newbie to python and I have an issue with converting some csv data to json data.

So here is the data in csv

RAW Data

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

enter image description here

EDIT 1: Link to sample excel file

Pivot Data

Pivot of this data translates like this. Basically it groups the profile and list all ids associated with it

enter image description here

I am trying to use python and pandas to convert the above data into following format:

   [
    {
        "profile": "SG-1234-BOM-A",
        "ids": ["8695e561-....639", "a65744ae-....9cb54"]
    },
    {
        "profile": "SG-4567-PUN02",
        "ids": ["6ee0f559-....96b08"]   
    },
    {
        "profile": "SG-7527-DEL02",
        "ids": ["6f2c.....62ba", "78f2.....fc939", "8885....0dc"]   
    }
   ]

I have tried to use following codes as well:

gp = df.groupby(['profile']).apply(lambda x: x.to_json(orient='records')).to_dict()

No luck with anything I tried. I have 10000+ rows that needs to be formatted in json. Any help would be appreciated! thanks in advance

>Solution :

Use the following approach:

df.groupby('profile')['id'].apply(list)\
    .reset_index(name='ids').to_dict(orient='records')

[{'profile': 'SG-1234-BOM-A',
  'ids': ['8298-hdfbs-37483-s', 'dhduf-7435-4897h-a']},
 {'profile': 'SG-4567-PUN02', 'ids': ['game-uio-09349-831']},
 {'profile': 'SG-7527-DEL02',
  'ids': ['2342-5648-9637-abc',
   '4587-9721-4547-ytf',
   '4654-a123-b789-09d',
   '1234-gt3g-56qw-321',
   '9897-93729-fghb-df',
   'uidr-9087-tyip-012',
   'hexa-tata-1234-fd3']}]
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