ComenzarEmpieza gratis

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

Ver curso

Instrucciones del ejercicio

  • 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().

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'].____().____()
Editar y ejecutar código