我正在使用 yahoofinancials 模塊基于基本指標創建股票篩選器。
下面的代碼以多維字典格式提供輸出,我無法將其轉換為資料幀格式以供進一步分析。
import pandas as pd
from yahoofinancials import YahooFinancials
ticker = 'RELIANCE.NS'
yahoo_financials = YahooFinancials(ticker)
income_statement_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'income')
income_statement_data_qt
輸出:

理想情況下,我想要這種格式的資料。

uj5u.com熱心網友回復:
您可以使用串列理解來迭代該特定字典中的字典,ticker并使用 Pandasconcat沿列軸 ( axis=1)連接資料。然后,使用rename_axis和reset_index將索引轉換為具有所需名稱的列。ticker使用insert.在第一個位置創建一個名稱為的新列。
import pandas as pd
from yahoofinancials import YahooFinancials
ticker = 'RELIANCE.NS'
yahoo_financials = YahooFinancials(ticker)
income_statement_data_qt = yahoo_financials.get_financial_stmts('quarterly', 'income')
dict_list = income_statement_data_qt['incomeStatementHistoryQuarterly'][ticker]
df = pd.concat([pd.DataFrame(i) for i in dict_list], axis=1)
df = df.rename_axis('incomeStatementHistoryQuarterly').reset_index()
df.insert(0, 'ticker', ticker)
print(df)
df 的輸出
ticker incomeStatementHistoryQuarterly ... 2021-03-31 2020-12-31
0 RELIANCE.NS costOfRevenue ... 1.034690e 12 7.224900e 11
1 RELIANCE.NS discontinuedOperations ... NaN NaN
2 RELIANCE.NS ebit ... 1.571800e 11 1.490100e 11
3 RELIANCE.NS effectOfAccountingCharges ... NaN NaN
...
...
18 RELIANCE.NS sellingGeneralAdministrative ... 3.976000e 10 4.244000e 10
19 RELIANCE.NS totalOperatingExpenses ... 1.338570e 12 1.029590e 12
20 RELIANCE.NS totalOtherIncomeExpenseNet ... -1.330000e 09 2.020000e 09
21 RELIANCE.NS totalRevenue ... 1.495750e 12 1.178600e 12
[22 rows x 6 columns]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/365745.html
上一篇:將每個串列元素轉換為嵌套的字典鍵
