I am calling an API that returns a batch request of multiple stock tickers in JSON format. It is a nested dictionary, with 2 levels of keys and then a list of dictionaries. Here is the script:
import json
import requests
import pandas as pd
r = requests.get('https://api.iextrading.com/1.0/stock/market/batch?symbols=aapl,wpx,mnro,twnk,labl,plnt,fsct,qyls,vrns,tree&types=chart&range=3m')
x = r.json()
llist = [*x.keys()]
df=[]
for l in llist:
df.append(pd.DataFrame.from_dict({(i,j): x[i][j]
for i in x.keys()
for j in x[i].keys()},
orient='index'))
df_concat = pd.concat(df, axis=1)
Here is the sample format:
{'AAPL': {'chart': [{'change': 0.129548,
'changeOverTime': 0,
'changePercent': 0.06,
'close': 217.6107,
'date': '2018-09-19',
'high': 218.8564,
'label': 'Sep 19, 18',
'low': 214.5514,
'open': 217.7403,
'unadjustedVolume': 27123833,
'volume': 27123833,
'vwap': 216.6509}
I would like to create a dataframe from this where everything is grouped by Ticker and ordered by Date with the other variables, such as: 'open', 'close', 'volume' used as the columns with the associated values in the row. So far the sample output looks like this:
0 1
(AAPL, chart) {'volume': 27123833, {'volume': 26608794,
'close': 118.21,... 'close': 120.11,...
(WPX, chart) {'volume': 1098766, {'volume': 993465,
'close': 13.23,... 'close': 14.68,...
What is the best and most efficient way about cleaning up this data and "unlocking" the list of dictionaries? Thanks in advance!