需要從 alphavantage API 決議股票代碼(以 JSON 格式)的季度收益并轉換為資料框。我究竟做錯了什么?
詳情如下:
輸出:
[{'symbol': 'AMZN', 'annualReports': [{'fiscalDateEnding': '2021-12-31', 'reportedCurrency': 'USD', 'grossProfit': '1269', 'totalRevenue': '4679'},
{'fiscalDateEnding': '2020-12-31', 'reportedCurrency': 'USD', 'grossProfit': '1025', 'totalRevenue': '3844'},
], 'quarterlyReports': [{'fiscalDateEnding': '2022-06-30', 'reportedCurrency': 'USD', 'grossProfit': '3959', 'totalRevenue': '1205'},
{'fiscalDateEnding': '2022-03-31', 'reportedCurrency': 'USD', 'grossProfit': '3701', 'totalRevenue': '1159'}]}]
想:
fiscalDateEnding reportedCurrency grossProfit totalRevenue
6/30/2022 USD 3959 1205
3/31/2022 USD 3701 1159
代碼:
i = 'AMZN'
#endpoint for hitting alphavantage api
income_statement_url = ["https://www.alphavantage.co/query?function=INCOME_STATEMENT&symbol=%s&apikey=APIKEY" % (i)]
#json response
income_statement_json = [(json.loads(requests.get(i).text)) for i in income_statement_url]
income_statement_df = [(pd.DataFrame.from_dict(i['totalRevenue'], orient = 'index').sort_index(axis = 1).assign(ticker = i['symbol']['quarterlyReports'])) for i in price_json]
錯誤資訊:
Heading KeyError: 'totalRevenue'
uj5u.com熱心網友回復:
你想要.json_normalize
df = pd.json_normalize(data=data, record_path="quarterlyReports")
print(df)
fiscalDateEnding reportedCurrency grossProfit totalRevenue
0 2022-06-30 USD 3959 1205
1 2022-03-31 USD 3701 1159
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/512193.html
上一篇:一個空的正則運算式如何計算?