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
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 tocomponents
. - Print
components
, sorted in descending order by market cap. - Select
Stock Symbol
from theindex
ofcomponents
, assign it totickers
and print the result. - Create a list
info_cols
that holds the column namesCompany Name
,Market Capitalization
, andLast Sale
. Next, use.loc[]
withtickers
andinfo_cols
toprint()
more details about the listings sorted in descending order byMarket 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(____)