Exercise

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.

Instructions

100 XP
  • Set 'Stock Symbol' as the index for listings, assigning it to listings_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 to top_3_companies.
  • Convert the index of the result to a list and assign it to top_3_tickers.
  • Use date() to set start to January 1, 2015.
  • Use date() to set end to April 1, 2020.
  • Use the DataReader() to get the stock data for the top_3_tickers from 'iex' since start until end and assign it to result.
  • Apply the .stack() method to convert the DataFrame to long format by moving the tickers into the index.
  • Select 'close' from data, apply .unstack(), and inspect the resulting DataFrame, now in wide format, with .info().