Get data for the 3 largest financial companies
A pd.MultiIndex() object has more than one identifier per row. This allows you to get the data based on criteria for multiple companies at once.
Let's apply this new skill to get the stock prices for the largest companies in the financial sector. DataReader, date, pandas as pd, and matplotlib.pyplot as plt have been imported, as has the listings DataFrame from the last exercise.
Cet exercice fait partie du cours
Importing and Managing Financial Data in Python
Instructions
- Set
'Stock Symbol'as the index forlistings, assigning it tolistings_ss. - Use
.loc[]to filter rows where the company sector is'Finance'and extract the'Market Capitalization'column. Apply.nlargest()to assign the 3 largest companies by market cap totop_3_companies. - Convert the index of the result to a list and assign it to
top_3_tickers. - Use
date()to setstartto January 1, 2015. - Use
date()to setendto April 1, 2020. - Use the
DataReader()to get the stock data for thetop_3_tickersfrom'iex'sincestartuntilendand assign it toresult. - Apply the
.stack()method to convert theDataFrameto long format by moving the tickers into the index. - Select
'close'fromdata, apply.unstack(), and inspect the resulting DataFrame, now in wide format, with.info().
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Set Stock Symbol as the index
listings_ss = listings.____
# Get ticker of 3 largest finance companies
top_3_companies = listings_ss.loc[____].____(n=____)
# Convert index to list
top_3_tickers = top_3_companies.____.____()
# Set start date
start = ____
# Set end date
end = ____
# Import stock data
result = ____
# Apply stack method
data = ____
# Unstack and inspect result
data['close'].____().____()