object has no attribute 'results' polygon.io API - python
I'm pulling down a bunch of tickers from the polygon.io:
from polygon import RESTClient
key = 'KEY'
all_tickers = []
with RESTClient(key) as client:
next_url = None
while True:
if next_url is None:
tickers = client.reference_tickers_v3(type="CS")
else:
tickers = client._handle_response("ReferenceTickersV3ApiResponse", next_url, {})
all_tickers.extend(tickers.results)
if hasattr(tickers, 'next_url'):
next_url = tickers.next_url
else:
break
print(all_tickers)
I'm then trying to loop through the tickers and pull in the daily data for each one:
with RESTClient(key) as client:
from_ = "2022-01-01"
to = "2022-02-01"
for i in all_tickers:
ticker = i['ticker']
resp = client.stocks_equities_aggregates(ticker, 1, "day", from_, to, unadjusted=False)
print(f"Minute aggregates for {resp.ticker} between {from_} and {to}.")
for result in resp.results:
if result is None:
print('nothing')
else:
print(result, 'result')
My problem is I'm getting the following error:
AttributeError: 'StocksEquitiesAggregatesApiResponse' object has no attribute 'results'
Here's my output:
Minute aggregates for A between 2022-01-01 and 2022-02-01.
{'v': 1606323.0, 'vw': 155.8021, 'o': 159, 'c': 156.48, 'h': 159.44, 'l': 153.93, 't': 1641186000000, 'n': 24318} result
{'v': 2233958.0, 'vw': 151.518, 'o': 155.49, 'c': 151.19, 'h': 155.63, 'l': 149.7, 't': 1641272400000, 'n': 34707} result
{'v': 2370529.0, 'vw': 149.9716, 'o': 150.83, 'c': 148.6, 'h': 153.1, 'l': 148.53, 't': 1641358800000, 'n': 27421} result
{'v': 2298277.0, 'vw': 148.4397, 'o': 148.85, 'c': 149.12, 'h': 149.96, 'l': 145.58, 't': 1641445200000, 'n': 34441} result
{'v': 2058658.0, 'vw': 146.4352, 'o': 149.12, 'c': 145.15, 'h': 149.73, 'l': 145.09, 't': 1641531600000, 'n': 28611} result
{'v': 2548145.0, 'vw': 143.2162, 'o': 143.29, 'c': 145.16, 'h': 145.31, 'l': 140.86, 't': 1641790800000, 'n': 37241} result
{'v': 2194208.0, 'vw': 146.0091, 'o': 145, 'c': 146.64, 'h': 146.94, 'l': 143.81, 't': 1641877200000, 'n': 22781} result
{'v': 2250847.0, 'vw': 149.3025, 'o': 147.8, 'c': 149.51, 'h': 150.39, 'l': 147.55, 't': 1641963600000, 'n': 27392} result
{'v': 1741764.0, 'vw': 145.7333, 'o': 149.46, 'c': 145.17, 'h': 149.54, 'l': 144.85, 't': 1642050000000, 'n': 23437} result
{'v': 2225442.0, 'vw': 143.9446, 'o': 144.04, 'c': 144.68, 'h': 145.15, 'l': 142.36, 't': 1642136400000, 'n': 28295} result
{'v': 1907368.0, 'vw': 141.2762, 'o': 142.42, 'c': 140.47, 'h': 143.24, 'l': 140.34, 't': 1642482000000, 'n': 27031} result
{'v': 1472206.0, 'vw': 141.538, 'o': 140.67, 'c': 140.43, 'h': 143.6, 'l': 140.26, 't': 1642568400000, 'n': 23595} result
{'v': 1861384.0, 'vw': 140.9367, 'o': 141.38, 'c': 139.48, 'h': 143.14, 'l': 139.05, 't': 1642654800000, 'n': 26612} result
{'v': 1878663.0, 'vw': 138.4591, 'o': 139.54, 'c': 137.51, 'h': 140.49, 'l': 137.49, 't': 1642741200000, 'n': 27133} result
{'v': 2155299.0, 'vw': 135.3192, 'o': 136.38, 'c': 138.12, 'h': 138.49, 'l': 131.28, 't': 1643000400000, 'n': 32745} result
{'v': 1705313.0, 'vw': 134.473, 'o': 135.36, 'c': 134.57, 'h': 136.62, 'l': 132.65, 't': 1643086800000, 'n': 24457} result
{'v': 1999575.0, 'vw': 134.5836, 'o': 135.54, 'c': 133.51, 'h': 138.0454, 'l': 132.27, 't': 1643173200000, 'n': 28088} result
{'v': 1715819.0, 'vw': 133.1775, 'o': 135.28, 'c': 132.09, 'h': 136.36, 'l': 131.68, 't': 1643259600000, 'n': 25556} result
{'v': 2174805.0, 'vw': 135.3363, 'o': 133, 'c': 137.06, 'h': 137.4, 'l': 131.215, 't': 1643346000000, 'n': 21446} result
{'v': 1702950.0, 'vw': 138.8672, 'o': 137.32, 'c': 139.32, 'h': 139.47, 'l': 136.9729, 't': 1643605200000, 'n': 21984} result
{'v': 1655987.0, 'vw': 140.2601, 'o': 140.53, 'c': 141.03, 'h': 141.27, 'l': 138.45, 't': 1643691600000, 'n': 25755} result
Minute aggregates for AA between 2022-01-01 and 2022-02-01.
{'v': 6208442.0, 'vw': 60.9882, 'o': 60.24, 'c': 60.36, 'h': 62.61, 'l': 60.09, 't': 1641186000000, 'n': 49209} result
{'v': 7943653.0, 'vw': 58.135, 'o': 60.68, 'c': 57.53, 'h': 61.15, 'l': 57.21, 't': 1641272400000, 'n': 59085} result
{'v': 7599751.0, 'vw': 60.0291, 'o': 58.95, 'c': 58.55, 'h': 61.79, 'l': 58.445, 't': 1641358800000, 'n': 65793} result
{'v': 4363058.0, 'vw': 58.3964, 'o': 58.94, 'c': 58.45, 'h': 59.4911, 'l': 57.25, 't': 1641445200000, 'n': 39017} result
{'v': 8071270.0, 'vw': 61.7246, 'o': 60.14, 'c': 62.37, 'h': 62.89, 'l': 59.65, 't': 1641531600000, 'n': 62728} result
{'v': 5653472.0, 'vw': 61.268, 'o': 61.62, 'c': 61.54, 'h': 62.71, 'l': 60.441, 't': 1641790800000, 'n': 43504} result
{'v': 6003582.0, 'vw': 61.0876, 'o': 60.71, 'c': 62.2, 'h': 62.25, 'l': 59.12, 't': 1641877200000, 'n': 50028} result
{'v': 6434989.0, 'vw': 62.1745, 'o': 63.66, 'c': 61.88, 'h': 64.37, 'l': 60.86, 't': 1641963600000, 'n': 53063} result
{'v': 5769838.0, 'vw': 61.5961, 'o': 61.75, 'c': 60.51, 'h': 63.26, 'l': 60.37, 't': 1642050000000, 'n': 46975} result
{'v': 4397108.0, 'vw': 60.5607, 'o': 60.27, 'c': 61.39, 'h': 61.44, 'l': 59.34, 't': 1642136400000, 'n': 41558} result
{'v': 5994091.0, 'vw': 60.0163, 'o': 60.5, 'c': 60.05, 'h': 61.56, 'l': 58.8, 't': 1642482000000, 'n': 55228} result
{'v': 7851084.0, 'vw': 60.0468, 'o': 61.39, 'c': 59.63, 'h': 61.93, 'l': 58.885, 't': 1642568400000, 'n': 62775} result
{'v': 15925959.0, 'vw': 62.0662, 'o': 62.1, 'c': 61.25, 'h': 64.25, 'l': 59.97, 't': 1642654800000, 'n': 127187} result
{'v': 11024982.0, 'vw': 57.5373, 'o': 60.02, 'c': 56.21, 'h': 60.15, 'l': 56.04, 't': 1642741200000, 'n': 99811} result
{'v': 9209629.0, 'vw': 56.091, 'o': 53.81, 'c': 58.02, 'h': 58.2, 'l': 53.26, 't': 1643000400000, 'n': 83494} result
{'v': 7780587.0, 'vw': 59.9004, 'o': 57.51, 'c': 61.21, 'h': 61.6, 'l': 56.7608, 't': 1643086800000, 'n': 66222} result
{'v': 9267426.0, 'vw': 61.8703, 'o': 61.54, 'c': 60.75, 'h': 63.64, 'l': 59.88, 't': 1643173200000, 'n': 76224} result
{'v': 6445290.0, 'vw': 59.0007, 'o': 60.6, 'c': 58.03, 'h': 61.6599, 'l': 57.47, 't': 1643259600000, 'n': 55506} result
{'v': 6987869.0, 'vw': 56.9155, 'o': 58, 'c': 57.4, 'h': 58.39, 'l': 55.58, 't': 1643346000000, 'n': 62130} result
{'v': 7206053.0, 'vw': 56.1436, 'o': 56.94, 'c': 56.71, 'h': 57.02, 'l': 55.02, 't': 1643605200000, 'n': 59567} result
{'v': 5939348.0, 'vw': 57.6426, 'o': 57.99, 'c': 58.17, 'h': 58.44, 'l': 56.73, 't': 1643691600000, 'n': 55371} result
Minute aggregates for AAC between 2022-01-01 and 2022-02-01.
{'v': 205721, 'vw': 9.7563, 'o': 9.75, 'c': 9.76, 'h': 9.77, 'l': 9.75, 't': 1641186000000, 'n': 272} result
{'v': 1523384.0, 'vw': 9.751, 'o': 9.77, 'c': 9.78, 'h': 9.78, 'l': 9.745, 't': 1641272400000, 'n': 1431} result
{'v': 97970, 'vw': 9.7777, 'o': 9.75, 'c': 9.79, 'h': 9.79, 'l': 9.75, 't': 1641358800000, 'n': 413} result
{'v': 38331, 'vw': 9.7725, 'o': 9.79, 'c': 9.76, 'h': 9.79, 'l': 9.75, 't': 1641445200000, 'n': 499} result
{'v': 216378, 'vw': 9.7519, 'o': 9.75, 'c': 9.77, 'h': 9.78, 'l': 9.75, 't': 1641531600000, 'n': 138} result
{'v': 16220, 'vw': 9.7657, 'o': 9.79, 'c': 9.77, 'h': 9.79, 'l': 9.75, 't': 1641790800000, 'n': 211} result
{'v': 9403, 'vw': 9.7541, 'o': 9.76, 'c': 9.77, 'h': 9.77, 'l': 9.75, 't': 1641877200000, 'n': 179} result
{'v': 9311, 'vw': 9.7626, 'o': 9.76, 'c': 9.76, 'h': 9.77, 'l': 9.76, 't': 1641963600000, 'n': 103} result
{'v': 33525, 'vw': 9.7611, 'o': 9.78, 'c': 9.76, 'h': 9.78, 'l': 9.76, 't': 1642050000000, 'n': 642} result
{'v': 211170, 'vw': 9.7413, 'o': 9.76, 'c': 9.74, 'h': 9.76, 'l': 9.74, 't': 1642136400000, 'n': 359} result
{'v': 215143, 'vw': 9.7316, 'o': 9.73, 'c': 9.73, 'h': 9.75, 'l': 9.7, 't': 1642482000000, 'n': 546} result
{'v': 232058, 'vw': 9.7308, 'o': 9.73, 'c': 9.74, 'h': 9.74, 'l': 9.73, 't': 1642568400000, 'n': 773} result
{'v': 113445, 'vw': 9.7337, 'o': 9.73, 'c': 9.74, 'h': 9.75, 'l': 9.73, 't': 1642654800000, 'n': 408} result
{'v': 1164821.0, 'vw': 9.7302, 'o': 9.74, 'c': 9.74, 'h': 9.74, 'l': 9.72, 't': 1642741200000, 'n': 544} result
{'v': 143194, 'vw': 9.7235, 'o': 9.72, 'c': 9.7, 'h': 9.74, 'l': 9.7, 't': 1643000400000, 'n': 569} result
{'v': 771480, 'vw': 9.702, 'o': 9.7, 'c': 9.72, 'h': 9.73, 'l': 9.7, 't': 1643086800000, 'n': 481} result
{'v': 155088, 'vw': 9.7176, 'o': 9.7111, 'c': 9.71, 'h': 9.73, 'l': 9.71, 't': 1643173200000, 'n': 533} result
{'v': 2597988.0, 'vw': 9.7081, 'o': 9.72, 'c': 9.715, 'h': 9.72, 'l': 9.7, 't': 1643259600000, 'n': 459} result
{'v': 32932, 'vw': 9.7059, 'o': 9.7, 'c': 9.72, 'h': 9.72, 'l': 9.7, 't': 1643346000000, 'n': 157} result
{'v': 34893, 'vw': 9.7202, 'o': 9.71, 'c': 9.72, 'h': 9.73, 'l': 9.71, 't': 1643605200000, 'n': 129} result
{'v': 49625, 'vw': 9.7323, 'o': 9.71, 'c': 9.74, 'h': 9.75, 'l': 9.71, 't': 1643691600000, 'n': 269} result
Minute aggregates for AACI between 2022-01-01 and 2022-02-01.
{'v': 112300, 'vw': 9.8232, 'o': 9.81, 'c': 9.91, 'h': 9.91, 'l': 9.81, 't': 1641186000000, 'n': 66} result
{'v': 1799, 'vw': 9.8753, 'o': 9.87, 'c': 9.9, 'h': 9.9, 'l': 9.865, 't': 1641272400000, 'n': 12} result
{'v': 447937, 'vw': 9.8, 'o': 9.8, 'c': 9.8, 'h': 9.8, 'l': 9.8, 't': 1641445200000, 'n': 5} result
{'v': 970, 'vw': 9.8643, 'o': 9.89, 'c': 9.87, 'h': 9.89, 'l': 9.8108, 't': 1641531600000, 'n': 12} result
{'v': 8345, 'vw': 9.8711, 'o': 9.84, 'c': 9.88, 'h': 9.89, 'l': 9.84, 't': 1641790800000, 'n': 35} result
{'v': 768, 'vw': 9.8518, 'o': 9.85, 'c': 9.85, 'h': 9.85, 'l': 9.85, 't': 1641877200000, 'n': 12} result
{'v': 37576, 'vw': 9.8501, 'o': 9.85, 'c': 9.86, 'h': 9.86, 'l': 9.85, 't': 1642050000000, 'n': 16} result
{'v': 1510, 'vw': 9.853, 'o': 9.85, 'c': 9.85, 'h': 9.8545, 'l': 9.85, 't': 1642136400000, 'n': 10} result
{'v': 17996, 'vw': 9.8423, 'o': 9.864, 'c': 9.85, 'h': 9.864, 'l': 9.84, 't': 1642482000000, 'n': 69} result
{'v': 21766, 'vw': 9.85, 'o': 9.8564, 'c': 9.85, 'h': 9.8564, 'l': 9.84, 't': 1642568400000, 'n': 20} result
{'v': 103, 'vw': 9.8401, 'o': 9.84, 'c': 9.84, 'h': 9.84, 'l': 9.84, 't': 1642654800000, 'n': 3} result
{'v': 204116, 'vw': 9.8142, 'o': 9.84, 'c': 9.81, 'h': 9.8404, 'l': 9.8, 't': 1642741200000, 'n': 716} result
{'v': 2315, 'vw': 9.822, 'o': 9.85, 'c': 9.8167, 'h': 9.87, 'l': 9.8167, 't': 1643000400000, 'n': 9} result
{'v': 12334, 'vw': 9.802, 'o': 9.825, 'c': 9.8, 'h': 9.825, 'l': 9.8, 't': 1643086800000, 'n': 26} result
{'v': 445, 'vw': 9.8, 'o': 9.8, 'c': 9.8, 'h': 9.8, 'l': 9.8, 't': 1643259600000, 'n': 5} result
{'v': 34013, 'vw': 9.8559, 'o': 9.83, 'c': 9.91, 'h': 9.95, 'l': 9.83, 't': 1643691600000, 'n': 124} result
Minute aggregates for AADI between 2022-01-01 and 2022-02-01.
{'v': 62017, 'vw': 24.6177, 'o': 24.46, 'c': 24.97, 'h': 25, 'l': 23.67, 't': 1641186000000, 'n': 1515} result
{'v': 106127, 'vw': 24.5493, 'o': 24.97, 'c': 24.49, 'h': 25.13, 'l': 24.16, 't': 1641272400000, 'n': 1909} result
{'v': 71124, 'vw': 23.1894, 'o': 24.5, 'c': 22.69, 'h': 24.6, 'l': 22.67, 't': 1641358800000, 'n': 1383} result
{'v': 131714, 'vw': 22.5708, 'o': 22.69, 'c': 22.58, 'h': 23.29, 'l': 22.05, 't': 1641445200000, 'n': 2686} result
{'v': 55025, 'vw': 22.0268, 'o': 22.44, 'c': 21.75, 'h': 22.74, 'l': 21.6, 't': 1641531600000, 'n': 1488} result
{'v': 65801, 'vw': 21.4205, 'o': 21.28, 'c': 21.88, 'h': 22.08, 'l': 20.67, 't': 1641790800000, 'n': 1305} result
{'v': 144496, 'vw': 23.8096, 'o': 22.76, 'c': 24.03, 'h': 24.86, 'l': 22.36, 't': 1641877200000, 'n': 2219} result
{'v': 196387, 'vw': 24.6995, 'o': 24.05, 'c': 24, 'h': 25.355, 'l': 23.515, 't': 1641963600000, 'n': 3160} result
{'v': 121495, 'vw': 23.2696, 'o': 23.78, 'c': 23.26, 'h': 23.9, 'l': 22.68, 't': 1642050000000, 'n': 2284} result
{'v': 142106, 'vw': 24.0981, 'o': 23.2, 'c': 24.51, 'h': 24.8, 'l': 23, 't': 1642136400000, 'n': 2340} result
{'v': 274538, 'vw': 22.7217, 'o': 24, 'c': 21.92, 'h': 24, 'l': 21.91, 't': 1642482000000, 'n': 4084} result
{'v': 344249, 'vw': 20.8055, 'o': 21.9, 'c': 19.94, 'h': 21.9, 'l': 19.925, 't': 1642568400000, 'n': 4688} result
{'v': 181528, 'vw': 20.4276, 'o': 19.94, 'c': 19.7, 'h': 21.42, 'l': 19.7, 't': 1642654800000, 'n': 2668} result
{'v': 170526, 'vw': 19.1647, 'o': 19.65, 'c': 19, 'h': 19.65, 'l': 18.56, 't': 1642741200000, 'n': 3127} result
{'v': 341151, 'vw': 17.9706, 'o': 18.7, 'c': 18.47, 'h': 19.51, 'l': 17.15, 't': 1643000400000, 'n': 3135} result
{'v': 125024, 'vw': 19.0888, 'o': 18.16, 'c': 19.39, 'h': 19.73, 'l': 18.1, 't': 1643086800000, 'n': 2297} result
{'v': 173235, 'vw': 19.5451, 'o': 19.59, 'c': 18.86, 'h': 20.92, 'l': 18.7, 't': 1643173200000, 'n': 2778} result
{'v': 168137, 'vw': 18.6189, 'o': 19.25, 'c': 18.56, 'h': 19.49, 'l': 18.38, 't': 1643259600000, 'n': 2485} result
{'v': 193384, 'vw': 17.9062, 'o': 18.24, 'c': 17.79, 'h': 18.83, 'l': 17.511, 't': 1643346000000, 'n': 3317} result
{'v': 137629, 'vw': 18.8928, 'o': 17.73, 'c': 19, 'h': 19.32, 'l': 17.73, 't': 1643605200000, 'n': 2734} result
{'v': 96935, 'vw': 19.0682, 'o': 19.16, 'c': 19.07, 'h': 19.5323, 'l': 18.72, 't': 1643691600000, 'n': 2146} result
Minute aggregates for AAIC between 2022-01-01 and 2022-02-01.
{'v': 60702, 'vw': 3.5587, 'o': 3.5, 'c': 3.56, 'h': 3.59, 'l': 3.5, 't': 1641186000000, 'n': 385} result
{'v': 68084, 'vw': 3.5922, 'o': 3.55, 'c': 3.58, 'h': 3.61, 'l': 3.55, 't': 1641272400000, 'n': 515} result
{'v': 83051, 'vw': 3.5949, 'o': 3.58, 'c': 3.53, 'h': 3.635, 'l': 3.53, 't': 1641358800000, 'n': 577} result
{'v': 68775, 'vw': 3.588, 'o': 3.55, 'c': 3.58, 'h': 3.61, 'l': 3.55, 't': 1641445200000, 'n': 434} result
{'v': 101412, 'vw': 3.614, 'o': 3.61, 'c': 3.59, 'h': 3.64, 'l': 3.59, 't': 1641531600000, 'n': 574} result
{'v': 92862, 'vw': 3.5719, 'o': 3.55, 'c': 3.57, 'h': 3.596, 'l': 3.55, 't': 1641790800000, 'n': 693} result
{'v': 61334, 'vw': 3.5934, 'o': 3.55, 'c': 3.61, 'h': 3.62, 'l': 3.55, 't': 1641877200000, 'n': 516} result
{'v': 69223, 'vw': 3.6051, 'o': 3.62, 'c': 3.59, 'h': 3.62, 'l': 3.59, 't': 1641963600000, 'n': 472} result
{'v': 59478, 'vw': 3.6034, 'o': 3.6, 'c': 3.58, 'h': 3.63, 'l': 3.58, 't': 1642050000000, 'n': 435} result
{'v': 55555, 'vw': 3.546, 'o': 3.51, 'c': 3.58, 'h': 3.59, 'l': 3.51, 't': 1642136400000, 'n': 362} result
{'v': 40123, 'vw': 3.5546, 'o': 3.53, 'c': 3.52, 'h': 3.586, 'l': 3.52, 't': 1642482000000, 'n': 498} result
{'v': 99546, 'vw': 3.4961, 'o': 3.46, 'c': 3.47, 'h': 3.52, 'l': 3.46, 't': 1642568400000, 'n': 464} result
{'v': 112002, 'vw': 3.4914, 'o': 3.48, 'c': 3.48, 'h': 3.54, 'l': 3.47, 't': 1642654800000, 'n': 681} result
{'v': 142444, 'vw': 3.4243, 'o': 3.49, 'c': 3.41, 'h': 3.49, 'l': 3.4, 't': 1642741200000, 'n': 613} result
{'v': 185877, 'vw': 3.3053, 'o': 3.42, 'c': 3.36, 'h': 3.42, 'l': 3.25, 't': 1643000400000, 'n': 1020} result
{'v': 94305, 'vw': 3.39, 'o': 3.34, 'c': 3.44, 'h': 3.46, 'l': 3.32, 't': 1643086800000, 'n': 840} result
{'v': 73169, 'vw': 3.4575, 'o': 3.48, 'c': 3.39, 'h': 3.4901, 'l': 3.3832, 't': 1643173200000, 'n': 932} result
{'v': 112655, 'vw': 3.375, 'o': 3.4, 'c': 3.31, 'h': 3.43, 'l': 3.31, 't': 1643259600000, 'n': 702} result
{'v': 67119, 'vw': 3.2877, 'o': 3.34, 'c': 3.31, 'h': 3.34, 'l': 3.25, 't': 1643346000000, 'n': 795} result
{'v': 110155, 'vw': 3.4097, 'o': 3.3, 'c': 3.43, 'h': 3.43, 'l': 3.3, 't': 1643605200000, 'n': 867} result
{'v': 84388, 'vw': 3.4423, 'o': 3.4, 'c': 3.44, 'h': 3.48, 'l': 3.39, 't': 1643691600000, 'n': 1000} result
Minute aggregates for AAL between 2022-01-01 and 2022-02-01.
{'v': 42891313.0, 'vw': 18.7823, 'o': 18.23, 'c': 18.75, 'h': 19.18, 'l': 18.2, 't': 1641186000000, 'n': 162180} result
{'v': 29266598.0, 'vw': 19.09, 'o': 19.215, 'c': 19.02, 'h': 19.46, 'l': 18.8504, 't': 1641272400000, 'n': 119439} result
{'v': 34447891.0, 'vw': 18.9205, 'o': 19.13, 'c': 18.68, 'h': 19.385, 'l': 18.51, 't': 1641358800000, 'n': 133198} result
{'v': 19097700.0, 'vw': 18.6698, 'o': 18.89, 'c': 18.57, 'h': 19.0599, 'l': 18.35, 't': 1641445200000, 'n': 91482} result
{'v': 42399468.0, 'vw': 19.211, 'o': 18.75, 'c': 19.28, 'h': 19.59, 'l': 18.58, 't': 1641531600000, 'n': 158244} result
{'v': 25707705.0, 'vw': 18.8635, 'o': 19.24, 'c': 18.79, 'h': 19.39, 'l': 18.68, 't': 1641790800000, 'n': 117681} result
{'v': 22531542.0, 'vw': 19.001, 'o': 18.88, 'c': 19.02, 'h': 19.205, 'l': 18.71, 't': 1641877200000, 'n': 97960} result
{'v': 41140852.0, 'vw': 18.6807, 'o': 19.16, 'c': 18.5, 'h': 19.255, 'l': 18.35, 't': 1641963600000, 'n': 151521} result
{'v': 55911766.0, 'vw': 19.3729, 'o': 18.73, 'c': 19.34, 'h': 19.7597, 'l': 18.63, 't': 1642050000000, 'n': 197015} result
{'v': 44983137.0, 'vw': 18.6557, 'o': 19.11, 'c': 18.49, 'h': 19.16, 'l': 18.4, 't': 1642136400000, 'n': 166866} result
{'v': 39855909.0, 'vw': 18.0989, 'o': 18.23, 'c': 17.9, 'h': 18.61, 'l': 17.77, 't': 1642482000000, 'n': 153703} result
{'v': 36133149.0, 'vw': 17.5146, 'o': 17.9, 'c': 17.31, 'h': 17.91, 'l': 17.29, 't': 1642568400000, 'n': 165519} result
{'v': 54892831.0, 'vw': 17.102, 'o': 17.63, 'c': 16.76, 'h': 17.7326, 'l': 16.6829, 't': 1642654800000, 'n': 221617} result
{'v': 52547514.0, 'vw': 16.3931, 'o': 16.77, 'c': 16.3, 'h': 16.78, 'l': 16.09, 't': 1642741200000, 'n': 217949} result
{'v': 60008675.0, 'vw': 15.8661, 'o': 15.85, 'c': 16.41, 'h': 16.44, 'l': 15.38, 't': 1643000400000, 'n': 229504} result
{'v': 42471591.0, 'vw': 16.2901, 'o': 16.05, 'c': 16.46, 'h': 16.54, 'l': 15.89, 't': 1643086800000, 'n': 169713} result
{'v': 39331762.0, 'vw': 16.6158, 'o': 16.7, 'c': 16.24, 'h': 16.97, 'l': 16.135, 't': 1643173200000, 'n': 146204} result
{'v': 43507377.0, 'vw': 16.0433, 'o': 16.425, 'c': 15.78, 'h': 16.84, 'l': 15.6226, 't': 1643259600000, 'n': 172213} result
{'v': 51662636.0, 'vw': 15.3385, 'o': 15.67, 'c': 15.64, 'h': 15.78, 'l': 14.905, 't': 1643346000000, 'n': 203975} result
{'v': 35533507.0, 'vw': 16.1221, 'o': 15.55, 'c': 16.47, 'h': 16.47, 'l': 15.4, 't': 1643605200000, 'n': 136379} result
{'v': 30688782.0, 'vw': 16.7612, 'o': 16.56, 'c': 16.83, 'h': 16.965, 'l': 16.4114, 't': 1643691600000, 'n': 125323} result
Minute aggregates for AAMC between 2022-01-01 and 2022-02-01.
I figure this is because it gets to a ticker and then doesn't have the results property. What's the correct way to handle this exception?
API documentation: https://polygon.io/docs/stocks/get_v2_aggs_ticker__stocksticker__range__multiplier___timespan___from___to
I was using if statements (coming from a JS background), but had to use try-except instead:
with RESTClient(key) as client:
from_ = "2022-01-01"
to = "2022-02-01"
for i in all_tickers:
ticker = i['ticker']
resp = client.stocks_equities_aggregates(ticker, 1, "day", from_, to, unadjusted=False)
print(f"Minute aggregates for {resp.ticker} between {from_} and {to}.")
try:
for result in resp.results:
print(result, 'result')
except:
print('nothing')
Further reading: https://www.geeksforgeeks.org/python-attributeerror/
Related
Websocket data converted into dictionary in python
i have two(n) websocket messages input like this, separated by "==new msg==" (don't know why but some of the messages are longer..) ++Rcv raw: b'\x81~\x02\xff{"data":[{"c":null,"p":20926.99,"s":"BINANCE:BTCUSDT","t":1656017217026,"v":0.0008},{"c":null,"p":20927.45,"s":"BINANCE:BTCUSDT","t":1656017217056,"v":0.0005},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.001},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.00061},{"c":null,"p":20930.52,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.1},{"c":null,"p":20931.18,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.05098},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03654},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.03},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.01882}],"type":"trade"}' ++Rcv decoded: fin=1 opcode=1 data=b'{"data":[{"c":null,"p":20926.99,"s":"BINANCE:BTCUSDT","t":1656017217026,"v":0.0008},{"c":null,"p":20927.45,"s":"BINANCE:BTCUSDT","t":1656017217056,"v":0.0005},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.001},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.00061},{"c":null,"p":20930.52,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.1},{"c":null,"p":20931.18,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.05098},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03654},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.03},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.01882}],"type":"trade"}' ==new msg== ++Rcv raw: b'\x81~\x03\x03{"data":[{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.08477},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.1},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.25},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.07053},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217694,"v":0.08207},{"c":null,"p":20926.98,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.05098},{"c":null,"p":20926.31,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.1},{"c":null,"p":20925.5,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.16424},{"c":null,"p":20927.87,"s":"BINANCE:BTCUSDT","t":1656017217711,"v":0.00052},{"c":null,"p":20923.16,"s":"BINANCE:BTCUSDT","t":1656017217927,"v":0.0155}],"type":"trade"}' ++Rcv decoded: fin=1 opcode=1 data=b'{"data":[{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.08477},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.1},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.25},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.07053},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217694,"v":0.08207},{"c":null,"p":20926.98,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.05098},{"c":null,"p":20926.31,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.1},{"c":null,"p":20925.5,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.16424},{"c":null,"p":20927.87,"s":"BINANCE:BTCUSDT","t":1656017217711,"v":0.00052},{"c":null,"p":20923.16,"s":"BINANCE:BTCUSDT","t":1656017217927,"v":0.0155}],"type":"trade"}' ==new msg== and i have some code so far import websocket import json from collections import ChainMap y = [] c = [] def on_message(ws, message): print("==new msg==") y.append(json.loads(message)) def on_error(ws, error): print(error) def on_close(ws,close_status_code, close_reason): print("==closed==") print(len(y)) c = dict(ChainMap(*y)) print(c) def on_open(ws): ws.send('{"type":"subscribe","symbol":"BINANCE:BTCUSDT"}') if __name__ == "__main__": websocket.enableTrace(True) ws = websocket.WebSocketApp("wss://ws.finnhub.io?token=****ps2ad3iahju47mdg", on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever() the output is: ==closed== 2 {'data': [{'c': None, 'p': 20926.99, 's': 'BINANCE:BTCUSDT', 't': 1656017217026, 'v': 0.0008}, {'c': None, 'p': 20927.45, 's': 'BINANCE:BTCUSDT', 't': 1656017217056, 'v': 0.0005}, {'c': None, 'p': 20930.51, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.001}, {'c': None, 'p': 20930.51, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.00061}, {'c': None, 'p': 20930.52, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.1}, {'c': None, 'p': 20931.18, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.05098}, {'c': None, 'p': 20930.5, 's': 'BINANCE:BTCUSDT', 't': 1656017217201, 'v': 0.03}, {'c': None, 'p': 20930.5, 's': 'BINANCE:BTCUSDT', 't': 1656017217201, 'v': 0.03654}, {'c': None, 'p': 20927.9, 's': 'BINANCE:BTCUSDT', 't': 1656017217649, 'v': 0.03}, {'c': None, 'p': 20927.9, 's': 'BINANCE:BTCUSDT', 't': 1656017217649, 'v': 0.01882}], 'type': 'trade'} only from the first message..? The goal is to calculate the volume-weighted average price of trades made during a 1 min interval output should look like this: 2022-05-04 12:22:33 price:20926.99 volume:0.0008 2022-05-04 12:23:43 price:20926.99 volume:0.0058 2022-05-04 12:24:53 price:20927.99 volume:0.0608
How to pull in the values instead of the keys in Python?
Hi apologies for the noob question... I have written some code: with RESTClient(key) as client: from_ = "2022-01-01" to = "2022-02-01" for i in all_tickers: ticker = i['ticker'] resp = client.stocks_equities_aggregates(ticker, 1, "day", from_, to, unadjusted=False) print(f"Minute aggregates for {resp.ticker} between {from_} and {to}.") try: for result in resp.results: print(result) except: print('nothing') Which outputs: Minute aggregates for A between 2022-01-01 and 2022-02-01. {'v': 1606323.0, 'vw': 155.8021, 'o': 159, 'c': 156.48, 'h': 159.44, 'l': 153.93, 't': 1641186000000, 'n': 24318} {'v': 2233958.0, 'vw': 151.518, 'o': 155.49, 'c': 151.19, 'h': 155.63, 'l': 149.7, 't': 1641272400000, 'n': 34707} {'v': 2370529.0, 'vw': 149.9716, 'o': 150.83, 'c': 148.6, 'h': 153.1, 'l': 148.53, 't': 1641358800000, 'n': 27421} {'v': 2298277.0, 'vw': 148.4397, 'o': 148.85, 'c': 149.12, 'h': 149.96, 'l': 145.58, 't': 1641445200000, 'n': 34441} {'v': 2058658.0, 'vw': 146.4352, 'o': 149.12, 'c': 145.15, 'h': 149.73, 'l': 145.09, 't': 1641531600000, 'n': 28611} {'v': 2548145.0, 'vw': 143.2162, 'o': 143.29, 'c': 145.16, 'h': 145.31, 'l': 140.86, 't': 1641790800000, 'n': 37241} {'v': 2194208.0, 'vw': 146.0091, 'o': 145, 'c': 146.64, 'h': 146.94, 'l': 143.81, 't': 1641877200000, 'n': 22781} {'v': 2250847.0, 'vw': 149.3025, 'o': 147.8, 'c': 149.51, 'h': 150.39, 'l': 147.55, 't': 1641963600000, 'n': 27392} {'v': 1741764.0, 'vw': 145.7333, 'o': 149.46, 'c': 145.17, 'h': 149.54, 'l': 144.85, 't': 1642050000000, 'n': 23437} {'v': 2225442.0, 'vw': 143.9446, 'o': 144.04, 'c': 144.68, 'h': 145.15, 'l': 142.36, 't': 1642136400000, 'n': 28295} {'v': 1907368.0, 'vw': 141.2762, 'o': 142.42, 'c': 140.47, 'h': 143.24, 'l': 140.34, 't': 1642482000000, 'n': 27031} {'v': 1472206.0, 'vw': 141.538, 'o': 140.67, 'c': 140.43, 'h': 143.6, 'l': 140.26, 't': 1642568400000, 'n': 23595} {'v': 1861384.0, 'vw': 140.9367, 'o': 141.38, 'c': 139.48, 'h': 143.14, 'l': 139.05, 't': 1642654800000, 'n': 26612} {'v': 1878663.0, 'vw': 138.4591, 'o': 139.54, 'c': 137.51, 'h': 140.49, 'l': 137.49, 't': 1642741200000, 'n': 27133} {'v': 2155299.0, 'vw': 135.3192, 'o': 136.38, 'c': 138.12, 'h': 138.49, 'l': 131.28, 't': 1643000400000, 'n': 32745} {'v': 1705313.0, 'vw': 134.473, 'o': 135.36, 'c': 134.57, 'h': 136.62, 'l': 132.65, 't': 1643086800000, 'n': 24457} {'v': 1999575.0, 'vw': 134.5836, 'o': 135.54, 'c': 133.51, 'h': 138.0454, 'l': 132.27, 't': 1643173200000, 'n': 28088} {'v': 1715819.0, 'vw': 133.1775, 'o': 135.28, 'c': 132.09, 'h': 136.36, 'l': 131.68, 't': 1643259600000, 'n': 25556} {'v': 2174805.0, 'vw': 135.3363, 'o': 133, 'c': 137.06, 'h': 137.4, 'l': 131.215, 't': 1643346000000, 'n': 21446} {'v': 1702950.0, 'vw': 138.8672, 'o': 137.32, 'c': 139.32, 'h': 139.47, 'l': 136.9729, 't': 1643605200000, 'n': 21984} {'v': 1655987.0, 'vw': 140.2601, 'o': 140.53, 'c': 141.03, 'h': 141.27, 'l': 138.45, 't': 1643691600000, 'n': 25755} Minute aggregates for AA between 2022-01-01 and 2022-02-01. {'v': 6208442.0, 'vw': 60.9882, 'o': 60.24, 'c': 60.36, 'h': 62.61, 'l': 60.09, 't': 1641186000000, 'n': 49209} {'v': 7943653.0, 'vw': 58.135, 'o': 60.68, 'c': 57.53, 'h': 61.15, 'l': 57.21, 't': 1641272400000, 'n': 59085} {'v': 7599751.0, 'vw': 60.0291, 'o': 58.95, 'c': 58.55, 'h': 61.79, 'l': 58.445, 't': 1641358800000, 'n': 65793} {'v': 4363058.0, 'vw': 58.3964, 'o': 58.94, 'c': 58.45, 'h': 59.4911, 'l': 57.25, 't': 1641445200000, 'n': 39017} {'v': 8071270.0, 'vw': 61.7246, 'o': 60.14, 'c': 62.37, 'h': 62.89, 'l': 59.65, 't': 1641531600000, 'n': 62728} {'v': 5653472.0, 'vw': 61.268, 'o': 61.62, 'c': 61.54, 'h': 62.71, 'l': 60.441, 't': 1641790800000, 'n': 43504} {'v': 6003582.0, 'vw': 61.0876, 'o': 60.71, 'c': 62.2, 'h': 62.25, 'l': 59.12, 't': 1641877200000, 'n': 50028} {'v': 6434989.0, 'vw': 62.1745, 'o': 63.66, 'c': 61.88, 'h': 64.37, 'l': 60.86, 't': 1641963600000, 'n': 53063} {'v': 5769838.0, 'vw': 61.5961, 'o': 61.75, 'c': 60.51, 'h': 63.26, 'l': 60.37, 't': 1642050000000, 'n': 46975} {'v': 4397108.0, 'vw': 60.5607, 'o': 60.27, 'c': 61.39, 'h': 61.44, 'l': 59.34, 't': 1642136400000, 'n': 41558} {'v': 5994091.0, 'vw': 60.0163, 'o': 60.5, 'c': 60.05, 'h': 61.56, 'l': 58.8, 't': 1642482000000, 'n': 55228} {'v': 7851084.0, 'vw': 60.0468, 'o': 61.39, 'c': 59.63, 'h': 61.93, 'l': 58.885, 't': 1642568400000, 'n': 62775} {'v': 15925959.0, 'vw': 62.0662, 'o': 62.1, 'c': 61.25, 'h': 64.25, 'l': 59.97, 't': 1642654800000, 'n': 127187} {'v': 11024982.0, 'vw': 57.5373, 'o': 60.02, 'c': 56.21, 'h': 60.15, 'l': 56.04, 't': 1642741200000, 'n': 99811} {'v': 9209629.0, 'vw': 56.091, 'o': 53.81, 'c': 58.02, 'h': 58.2, 'l': 53.26, 't': 1643000400000, 'n': 83494} {'v': 7780587.0, 'vw': 59.9004, 'o': 57.51, 'c': 61.21, 'h': 61.6, 'l': 56.7608, 't': 1643086800000, 'n': 66222} {'v': 9267426.0, 'vw': 61.8703, 'o': 61.54, 'c': 60.75, 'h': 63.64, 'l': 59.88, 't': 1643173200000, 'n': 76224} {'v': 6445290.0, 'vw': 59.0007, 'o': 60.6, 'c': 58.03, 'h': 61.6599, 'l': 57.47, 't': 1643259600000, 'n': 55506} {'v': 6987869.0, 'vw': 56.9155, 'o': 58, 'c': 57.4, 'h': 58.39, 'l': 55.58, 't': 1643346000000, 'n': 62130} {'v': 7206053.0, 'vw': 56.1436, 'o': 56.94, 'c': 56.71, 'h': 57.02, 'l': 55.02, 't': 1643605200000, 'n': 59567} {'v': 5939348.0, 'vw': 57.6426, 'o': 57.99, 'c': 58.17, 'h': 58.44, 'l': 56.73, 't': 1643691600000, 'n': 55371} ... When I try to put that data in an array, like so: with RESTClient(key) as client: from_ = "2022-01-01" to = "2022-02-01" arr = [] for i in all_tickers: ticker = i['ticker'] resp = client.stocks_equities_aggregates(ticker, 1, "day", from_, to, unadjusted=False) print(f"Minute aggregates for {resp.ticker} between {from_} and {to}.") try: for result in resp.results: arr.extend(result) print(arr) except: print('nothing') It is just saving the keys: Minute aggregates for A between 2022-01-01 and 2022-02-01. ['v', 'vw', 'o', 'c', 'h', 'l', 't', 'n'] ['v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n'] ['v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n'] ['v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n'] How do I make it so the entire object is put into the array? When I say the entire object I mean all the keys and values, e.g: arr = [{'v': 6208442.0, 'vw': 60.9882, 'o': 60.24, 'c': 60.36, 'h': 62.61, 'l': 60.09, 't': 1641186000000, 'n': 49209}, {'v': 6208442.0, 'vw': 60.9882, 'o': 60.24, 'c': 60.36, 'h': 62.61, 'l': 60.09, 't': 1641186000000, 'n': 49209}, ...] Any idea what I'm doing wrong?
Instead of this: for result in resp.results: arr.extend(result) ... try this: for result in resp.results: arr.append(result) ... or this: arr.extend(resp.results) One of those (or possibly both) should work.
Get a value of key from JSON response in Python
I am accessing an API through a get request from which I get a response. This is my code: import requests import json symbol = "AAPL" end_point="https://api.polygon.io/v2/snapshot/locale/us/markets/stocks/tickers/"+symbol+"?apiKey=my_key" r=requests.get(end_point).json() print(r) print(r['min']) print(r) returned: {'request_id': '2735c0be51de7719fd99460fe8696080', 'status': 'OK', 'ticker': {'day': {'c': 172.93, 'h': 175.48, 'l': 172.37, 'o': 174.14, 'v': 65575561, 'vw': 174.0984}, 'lastQuote': {'P': 172.83, 'S': 3, 'p': 172.82, 's': 1, 't': 1644524332922450142}, 'lastTrade': {'c': None, 'i': '139592', 'p': 172.8199, 's': 2014, 't': 1644524331573573011, 'x': 4}, 'min': {'av': 65559987, 'c': 172.9, 'h': 173.14, 'l': 172.89, 'o': 173.09, 'v': 107429, 'vw': 173.0138}, 'prevDay': {'c': 176.28, 'h': 176.65, 'l': 174.9, 'o': 176.05, 'v': 71204538, 'vw': 175.8287}, 'ticker': 'AAPL', 'todaysChange': -3.46, 'todaysChangePerc': -1.963, 'updated': 1644524331573573011}} but when I try to access the key "min", I get key error: KeyError: 'min' This is super-simple. What am I doing wrong?
Please try: r['ticker']['min']
the key min is inside ticker. Do this: r['ticker']['min']
Dict in List in Dict to pandas dataframe
I think I am receiving a dict inside of a list inside of a dict from an API. How do I convert this to a pandas dataframe? When I use pandas.dataframe.from_dict() the output is just one of the dictionaries in each of the elements, instead of splitting up the dictionaries. a= {'count': 10465, 'status': 'DELAYED', 'tickers': [{'day': {'c': 48.11, 'h': 49.61, 'l': 48.11, 'o': 48.36, 'v': 2018543, 'vw': 48.6329}, 'lastQuote': {'P': 48.07, 'S': 50, 'p': 48.02, 's': 3, 't': 1619195328041448704}, 'lastTrade': {'c': None, 'i': '71683813093722', 'p': 48.04, 's': 100, 't': 1619195306417034752, 'x': 4}, 'min': {'av': 2018540, 'c': 48.11, 'h': 48.19, 'l': 48.11, 'o': 48.19, 'v': 8842, 'vw': 48.1468}, 'prevDay': {'c': 47.83, 'h': 49.54, 'l': 47.07, 'o': 47.9, 'v': 4938809, 'vw': 48.3369}, 'ticker': 'FTCH', 'todaysChange': 0.21, 'todaysChangePerc': 0.439, 'updated': 1619195306417034752}, {'day': {'c': 4.01, 'h': 4.09, 'l': 4.01, 'o': 4.03, 'v': 12077, 'vw': 4.0342}, 'lastQuote': {'P': 4.09, 'S': 3, 'p': 4, 's': 7, 't': 1619195239632140363}, 'lastTrade': {'c': [14, 41], 'i': '4', 'p': 4.01, 's': 100, 't': 1619195239632141482, 'x': 21}, 'min': {'av': 12077, 'c': 4.01, 'h': 4.01, 'l': 4.01, 'o': 4.01, 'v': 100, 'vw': 4.01}, 'prevDay': {'c': 4.03, 'h': 4.07, 'l': 4.01, 'o': 4.05, 'v': 39628, 'vw': 4.038}, 'ticker': 'METC', 'todaysChange': -0.02, 'todaysChangePerc': -0.496, 'updated': 1619195280000000000} ]} Expected result: Ticker day_c min_c FTCH 48.11 48.11 METC 4.01 4.01
If data is your dictionary from the question: df = pd.DataFrame( [ { "Ticker": t["ticker"], "day_c": t["day"]["c"], "min_c": t["min"]["c"], } for t in data["tickers"] ] ) print(df) Prints: Ticker day_c min_c 0 FTCH 48.11 48.11 1 METC 4.01 4.01
Using json_normalize with df.join and df.pop For earlier versions: out = pd.DataFrame(a) out = out.join(pd.io.json.json_normalize(out.pop('tickers'))) For latest versions: out = pd.DataFrame(a) out = out.join(pd.json_normalize(out.pop('tickers'))) Output: count status ticker todaysChange todaysChangePerc updated \ 0 10465 DELAYED FTCH 0.21 0.439 1619195306417034752 1 10465 DELAYED METC -0.02 -0.496 1619195280000000000 day.c day.h day.l day.o ... min.l min.o min.v min.vw prevDay.c \ 0 48.11 49.61 48.11 48.36 ... 48.11 48.19 8842 48.1468 47.83 1 4.01 4.09 4.01 4.03 ... 4.01 4.01 100 4.0100 4.03 prevDay.h prevDay.l prevDay.o prevDay.v prevDay.vw 0 49.54 47.07 47.90 4938809 48.3369 1 4.07 4.01 4.05 39628 4.0380
A bit of an overkill, but you could use jmespath; it comes in handy as your nested data becomes more nested. Basic Use: if accessing a key, use ., if traversing an array/list, use [] : extract = jmespath.search("""{ticker: tickers[].ticker, day_c: tickers[].day.c, min_c: tickers[].min.c} """, a) extract {'ticker': ['FTCH', 'METC'], 'day_c': [48.11, 4.01], 'min_c': [48.11, 4.01]} pd.DataFrame(extract) ticker day_c min_c 0 FTCH 48.11 48.11 1 METC 4.01 4.01 It is pretty fast as well, although nothing beats use of native/raw dictionaries
Manipulate a List in Python that Contains Other Lists
I am trying to extract information from a list within a list within a list to end up with something like this from the information below: ('h': '0.77584', 'l': '0.77292'), ('h': '0.77521', 'l': '0.77206') print(dict) [{'complete': True, 'volume': 2290, 'time': '2021-01-15', 'mid': {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}}, {'complete': True, 'volume': 2312, 'time': '2021-01-15', 'mid': {'o': '0.77436', 'h': '0.77521', 'l': '0.77206', 'c': '0.77206'}}] Not sure how to go about it. I tried something = list(list(dict.items())[0].items())[3][1] print(something) However, this returned {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'} How to get the requested data?
You can use the following list and dict comprehension dict = [{'complete': True, 'volume': 2290, 'time': '2021-01-15', 'mid': {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}}, {'complete': True, 'volume': 2312, 'time': '2021-01-15', 'mid': {'o': '0.77436', 'h': '0.77521', 'l': '0.77206', 'c': '0.77206'}}] res = [{k:v for k, v in i['mid'].items() if k in 'hl'} for i in dict] print(res) Output [{'h': '0.77584', 'l': '0.77292'}, {'h': '0.77521', 'l': '0.77206'}]
First you can take the mid child from each parent dictionary in the list. dict = [{'complete': True, 'volume': 2290, 'time': '2021-01-15', 'mid': {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}}, {'complete': True, 'volume': 2312, 'time': '2021-01-15', 'mid': {'o': '0.77436', 'h': '0.77521', 'l': '0.77206', 'c': '0.77206'}}] old_dict = [dict[0]['mid'],dict[1]['mid']] Then loop through those entries and filter based off the keys (i.e. remove o and c): list_you_want = [] for i in [0,1]: list_you_want.append({ your_key: old_dict[i][your_key] for your_key in ['h','l'] }) Which gives you: [{'h': '0.77584', 'l': '0.77292'}, {'h': '0.77521', 'l': '0.77206'}]