Skip to content Skip to sidebar Skip to footer

Creating Dataframe From Nested Dictionary

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

Solution 1:

This will do the job, not sure if it's the cleanest way:

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)

This will have the output:

changechangeOverTimechangePercentclosedatehighlabellowopentickerunadjustedVolumevolumevwap00.1295480.0000000.060217.61072018-09-19  218.8564Sep19,18214.5514217.7403AAPL27123833.027123833.0216.650911.6542000.0076020.760219.26502018-09-20  221.5071Sep20,18218.3880219.4742AAPL26608794.026608794.0219.99992-2.361800-0.003251-1.077216.90322018-09-21  220.5903Sep21,18216.5345220.0123AAPL96246748.096246748.0217.734733.1191000.0110821.438220.02232018-09-24  220.4907Sep24,18215.8768216.0661AAPL27693358.027693358.0218.6857

Post a Comment for "Creating Dataframe From Nested Dictionary"