我正在调用一个API,该API以JSON格式返回多个股票报价器的批处理请求。它是一个嵌套的字典,具有2个级别的键,然后是字典列表。这是脚本:
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)
以下是示例格式:
{'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}
我想从中创建一个数据框,其中所有内容均按代码行进行分组,并按日期与其他变量进行排序,例如:'open','close','volume'用作具有相关值的列行。到目前为止,示例输出如下:
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,...
清理这些数据和“解锁”词典列表的最佳和最有效的方法是什么?预先感谢!
答案 0 :(得分:0)
这可以完成工作,不确定是否是最干净的方法:
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()
output = pd.DataFrame()
for ticker, chart in x.items():
for k, v in chart.items():
for dictionary in v:
data = dictionary
data['ticker'] = ticker
output = output.append(data, ignore_index=True)
这将显示输出:
change changeOverTime changePercent close date high label low open ticker unadjustedVolume volume vwap
0 0.129548 0.000000 0.060 217.6107 2018-09-19 218.8564 Sep 19, 18 214.5514 217.7403 AAPL 27123833.0 27123833.0 216.6509
1 1.654200 0.007602 0.760 219.2650 2018-09-20 221.5071 Sep 20, 18 218.3880 219.4742 AAPL 26608794.0 26608794.0 219.9999
2 -2.361800 -0.003251 -1.077 216.9032 2018-09-21 220.5903 Sep 21, 18 216.5345 220.0123 AAPL 96246748.0 96246748.0 217.7347
3 3.119100 0.011082 1.438 220.0223 2018-09-24 220.4907 Sep 24, 18 215.8768 216.0661 AAPL 27693358.0 27693358.0 218.6857