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.
Este ejercicio forma parte del curso
Importing and Managing Financial Data in Python
Instrucciones del ejercicio
- 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 setstart
to January 1, 2015. - Use
date()
to setend
to April 1, 2020. - Use the
DataReader()
to get the stock data for thetop_3_tickers
from'iex'
sincestart
untilend
and assign it toresult
. - Apply the
.stack()
method to convert theDataFrame
to 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()
.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# 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'].____().____()