I'm creating a stock screener based on fundamental metrics using yahoofinancials module.
Below code gives output in multidimensional dictionary format that I'm not able to convert into dataframe format for further analysis.
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
Ideally, I'd like to have data in this format.
CodePudding user response:
You can use list comprehension to iterate over the dictionaries from that particular ticker
and use Pandas concat
to concatenate the data along the columns axis (axis=1
). Then, use rename_axis
and reset_index
to convert the index to a column with the desired name. Create a new column with the ticker
name at the first position using 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)
Output from 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]