The code below exports the latest annual Balance Sheet data. I trying to get all the available annual Balance Sheet data. Four years is usually available (e.g., GOOG). Credit to Luis Alejandro Vargas Ramos, who basically created all this code from my other questions (Question 1 & Question 2).
import pandas as pd
import yfinance as yf
import datetime
import time
companies = ['GOOG','MSFT','AAPL']
company_metrics = {}
for company in companies:
company_metrics[company] = {}
company_info = yf.Ticker(company)
company_metrics[company] = company_info.balance_sheet
results = []
for i in companies:
result = company_metrics[i].iloc[:, 0]
results.append(result)
#Then build the dataframe:
df = pd.DataFrame(results).T
df.columns = companies
#Lastly, build the excel:
df.to_excel('102722_5Yroutput.xlsx')
Code below pulls exactly what I want for a single stock ticker. Picture below is what I am trying to export for multiple tickers (data is duplicated for illustrative purposes).
import yfinance as yf
stock = yf.Ticker("MSFT")
stock.balance_sheet
I was able to accomplish this using yahooquery, which is really fast instead of yfinance.
import pandas as pd
import yfinance as yf
import datetime
import time
from yahooquery import Ticker
symbols = ['fb', 'aapl', 'amzn', 'nflx', 'goog']
faang = Ticker(symbols)
faang.balance_sheet()
#Then build the dataframe:
df = pd.DataFrame(faang.balance_sheet())
#df = pd.DataFrame(faang.balance_sheet()).T
#use bottom line if you want to transpose the data
df.to_excel('output.xlsx')