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 transform a list of jsons into a dataframe?

I have a list of jsons looking like this:

[
{
'1': [{'code': '7654','location': 'California'}]
},
{
'2': [{'code': '7834','location': 'Texas'},{'code': '3912','location': 'NYC'}]
}
]

I would like to have a pandas dataframe like this:

id value
1  [{'code': '7654','location': 'California'}]
2  [{'code': '7834','location': 'Texas'},{'code': '3912','location': 'NYC'}]

I tried with json normalize but the problem is the keys numbers 1 and 2.

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

How can I do ?

>Solution :

try,

import pandas as pd

values = [{
    '1': [{'code': '7654', 'location': 'California'}]
}, {
    '2': [{'code': '7834', 'location': 'Texas'}, {'code': '3912', 'location': 'NYC'}]
}]

pd.DataFrame(
    [{"id": k, "value": v} for value in values for k, v in value.items()]
)

  id                                              value
0  1       [{'code': '7654', 'location': 'California'}]
1  2  [{'code': '7834', 'location': 'Texas'}, {'code...
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