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 Multiple Nested JSON to Pandas Dataframe

Here’s the response I get from my stock data API provider:

    {'status': {'code': 0, 'message': ''},
 'data': {'symbol': 'ESHRAQ',
  'company': 'Eshraq properties Co.',
  'exchange': 'ABU_DHABI',
  'prices': {'columns': ['date', 'open', 'high', 'low', 'close', 'volume'],
   'values': [['2021-12-16T10:00:00Z', 0.39, 0.39, 0.39, 0.39, 4140513],
    ['2021-12-19T10:00:00Z', 0.0, 0.0, 0.35, 0.38, 19006953]]}}}

I need the results to be a simple Pandas dataframe with the following columns: date, company, open, high, low, close, volume.

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

>Solution :

d = your_json['data']
df = pd.DataFrame(d['prices']['values'], columns=d['prices']['columns']).assign(company=d['company'])

Output:

>>> df
                   date  open  high   low close    volume                company
0  2021-12-16T10:00:00Z  0.39  0.39  0.39  0.39   4140513  Eshraq properties Co.
1  2021-12-19T10:00:00Z  0.00  0.00  0.35  0.38  19006953  Eshraq properties Co.
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