【问题标题】:Creating dataframe from Nested Dictionary [duplicate]从嵌套字典创建数据框[重复]
【发布时间】:2019-05-22 07:47:52
【问题描述】:

我正在调用一个 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}

我想从中创建一个数据框,其中所有内容都按 Ticker 分组并按 Date 与其他变量排序,例如:'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,...

清理这些数据和“解锁”字典列表的最佳和最有效的方法是什么?提前致谢!

【问题讨论】:

  • 你能举一个你想要的数据输出的例子吗?

标签: python pandas api dictionary nested


【解决方案1】:

这将完成这项工作,不确定它是否是最干净的方式:

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

【讨论】:

  • 这太好了,谢谢!
猜你喜欢
  • 2021-11-16
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 2019-01-11
相关资源
最近更新 更多