Select and inspect index components

Now that you have imported and cleaned the listings data, you can proceed to select the index components as the largest company for each sector by market capitalization.

You'll also have the opportunity to take a closer look at the components, their last market value, and last price.

This exercise is part of the course

Manipulating Time Series Data in Python

View Course

Exercise instructions

We have already imported pandas as pd, and loaded the listings data with the modifications you made during the last exercise.

  • Use .groupby() and .nlargest() to select the largest company by 'Market Capitalization' for each 'Sector', and assign the result to components.
  • Print components, sorted in descending order by market cap.
  • Select Stock Symbol from the index of components, assign it to tickers and print the result.
  • Create a list info_cols that holds the column names Company Name, Market Capitalization, and Last Sale. Next, use .loc[] with tickers and info_cols to print() more details about the listings sorted in descending order by Market Capitalization).

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Select largest company for each sector
components = ____

# Print components, sorted by market cap
print(____)

# Select stock symbols and print the result
tickers = ____
print(____)

# Print company name, market cap, and last price for each component 
info_cols = ____
print(____)