简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM