Bogdan Titomir
Bogdan Titomir

Reputation: 337

Failing to convert JSON to pandas dataframe

I am new to python. I am trying to convert this json to pandas df.

request = {'earningsCalendar': [{'date': '2021-04-27', 'epsActual': None, 'epsEstimate': None, 'hour': 'bmo', 'quarter': 1, 'revenueActual': None, 'revenueEstimate': None, 'symbol': 'ALHC', 'year': 2021}, {'date': '2021-04-27', 'epsActual': None, 'epsEstimate': None, 'hour': 'amc', 'quarter': 1, 'revenueActual': None, 'revenueEstimate': None, 'symbol': 'FRST', 'year': 2021}]}

Whatever I do I cannot convert it properly. I have tried pandas.json_normalize and tried a special package flatten_json. Both of them did not help. I think the problem is because of the square brackets. But I don't know how to solve it.

I would appreciate any help.

Upvotes: 0

Views: 92

Answers (1)

sedavidw
sedavidw

Reputation: 11741

Assuming you want each field in the dictionaries to be a column you could do the following

import pandas as pd
earnings_calendar_df = pd.DataFrame(request['earningsCalendar'])

which yields

In [16]: earnings_calendar_df
Out[16]:
         date epsActual epsEstimate hour  quarter revenueActual revenueEstimate symbol  year
0  2021-04-27      None        None  bmo        1          None            None   ALHC  2021
1  2021-04-27      None        None  amc        1          None            None   FRST  2021

Upvotes: 4

Related Questions