I want to get net income and research development for the current year from multiple companies. At present, ticker.financials from yfinance library gives me the full table for 4 years and for multiple metrics.
I am checking below the keys for financials and there are only by years.
# Import packages
import yfinance as yf
import pandas as pd
msft = yf.Ticker("MSFT")
info = msft.financials
info.keys()
DatetimeIndex(['2021-06-30', '2020-06-30', '2019-06-30', '2018-06-30'], dtype='datetime64[ns]', name='', freq=None)
For example,
msft.financials
gives me this
I want to create a ticker group like
tickers = ["AAPL", "MSFT", "IBM", "GOOG", "ORCL"]
For these companies I need the net income and research and development for 2021-06-30.
How is this possible with yfinance?
CodePudding user response:
Since the format in which the company's financial information is obtained is a data frame, the latest closing date is combined into an empty data frame. After merging, update the column names in the ticker list. The following is a part of the dataframe after the retrieval. With the obtained data, we extract the necessary subjects for each.
import yfinance as yf
import pandas as pd
tickers = ["AAPL", "MSFT", "IBM", "GOOG", "ORCL"]
df = pd.DataFrame()
cols = []
for ticker in tickers:
company = yf.Ticker(ticker)
info = company.financials
df = pd.concat([df, info.iloc[:,0]], axis=1)
cols.append(ticker '(' info.columns[0].strftime('%Y-%m-%d') ')')
df.columns = cols
df.head()
AAPL(2021-09-25) MSFT(2021-06-30) IBM(2020-12-31) GOOG(2020-12-31) ORCL(2021-05-31)
Research Development 21914000000.0 20716000000.0 6333000000.0 27573000000.0 6527000000.0
Effect Of Accounting Charges None None None None None
Income Before Tax 109207000000.0 71102000000.0 4637000000.0 48082000000.0 12999000000.0
Minority Interest None None 129000000.0 None 714000000.0
Net Income 94680000000.0 61271000000.0 5590000000.0 40269000000.0 13746000000.0
Research Development & Income
df.iloc[[0,4],]
AAPL(2021-09-25) | MSFT(2021-06-30) | IBM(2020-12-31) | GOOG(2020-12-31) | ORCL(2021-05-31) | |
---|---|---|---|---|---|
Research Development | 2.1914e 10 | 2.0716e 10 | 6.333e 09 | 2.7573e 10 | 6.527e 09 |
Net Income | 9.468e 10 | 6.1271e 10 | 5.59e 09 | 4.0269e 10 | 1.3746e 10 |