Exporting Last Four Years’ yfinance Balance Sheet Results Into Single Excel Workbook?
Introduction
The yfinance library in Python is a popular tool for accessing financial and economic data from Yahoo Finance. One of the key features of this library is its ability to fetch balance sheet data for companies. However, fetching balance sheet data for multiple years can be cumbersome using the yfinance library alone. In this article, we will explore how to export last four years’ yfinance balance sheet results into a single Excel workbook.
Understanding the Limitations of yfinance
Before diving into the solution, it’s essential to understand the limitations of the yfinance library. The library only fetches the latest annual balance sheet data for companies. If you want to get all available annual balance sheet data for a company, you need to manually download and parse the data.
Using Yahooquery as an Alternative
One alternative approach is using the yahooquery library. This library provides a more efficient way of fetching balance sheet data from Yahoo Finance compared to yfinance. With yahooquery, you can easily fetch balance sheet data for multiple years and export it into a single Excel workbook.
In this article, we will focus on using the yahooquery library to solve the problem.
Installing the Required Libraries
Before proceeding with the solution, ensure that you have installed the required libraries. You can install them using pip:
pip install yfinance yahooquery pandas
Getting Started with Yahooquery
To get started with yahooquery, import the necessary libraries and define your company symbols.
Defining Company Symbols
In this example, we will focus on four companies: Facebook (FB), Apple (AAPL), Amazon (AMZN), Netflix (NFLX), and Google (GOOG).
import pandas as pd
from yahooquery import Ticker
symbols = ['fb', 'aapl', 'amzn', 'nflx', 'goog']
Fetching Balance Sheet Data with Yahooquery
Now that you have defined your company symbols, use the Ticker
object from the yahooquery
library to fetch balance sheet data.
faang = Ticker(symbols)
# Fetching balance sheet data for multiple years
balance_sheets = faang.balance_sheet()
Building a DataFrame from Balance Sheet Data
After fetching the balance sheet data, you need to build a DataFrame from it. We will use the to_frame
method to convert the balance sheets into a pandas DataFrame.
# Building a DataFrame from balance sheet data
df = pd.DataFrame(balance_sheets)
Transpose and Export Dataframe as Excel Workbook
If you want to transpose the data, use the T
attribute to swap rows with columns. Then, export the DataFrame as an Excel workbook using the to_excel
method.
# Transposing data
df = df.T
# Exporting dataframe as excel file
df.to_excel('output.xlsx')
Exporting Last Four Years’ Balance Sheet Data
To export last four years’ balance sheet data, you need to fetch the balance sheet data for each year separately. We will use the balance_sheet_history
attribute to get the historical balance sheets.
# Fetching historical balance sheet data
historical_sheets = faang.balance_sheet_history(period='4y')
# Building a DataFrame from historical balance sheet data
df = pd.DataFrame(historical_sheets)
# Transposing data
df = df.T
# Exporting dataframe as excel file
df.to_excel('output.xlsx')
Conclusion
In this article, we explored how to export last four years’ yfinance balance sheet results into a single Excel workbook using the yahooquery
library. We defined company symbols, fetched balance sheet data, built a DataFrame from it, and exported the data as an Excel workbook.
By following these steps, you can easily fetch historical balance sheet data for multiple companies and export it into a single Excel workbook.
Table of Contents
- Exporting Last Four Years’ yfinance Balance Sheet Results Into Single Excel Workbook?
Last modified on 2024-12-15