Get the ticker of the largest consumer services company
Instead of indexing your data with a conditional expression, you can also filter by certain values with .loc[row_selector, column_selector]
. Additionally, you can use .set_index()
to set a particular column with unique values as the index of a DataFrame, and .idxmax()
to return the index of the maximum value.
In this exercise, you will apply these methods of selecting companies to find the most valuable consumer services company on any of the three exchanges, and use its ticker to plot its stock price trend. DataReader
, date
, pandas
as pd
, and matplotlib.pyplot
as plt
have been imported, as has the listings
DataFrame from the last exercise.
Este exercício faz parte do curso
Importing and Managing Financial Data in Python
Instruções do exercício
- Use
.set_index()
to set the'Stock Symbol'
column as the index forlistings
, assigning it tolistings_ss
. - Use
.loc[]
to filter rows where'Sector'
is equal to'Consumer Services'
, select the column'Market Capitalization'
, and apply.idxmax()
to assign the ticker of the largest Consumer Services company toticker
. - Using
date()
, setstart
to January 1, 2015. - Use
DataReader()
to extract the stock data for theticker
from'yahoo'
sincestart
and store indata
. - Plot the
'close'
and'volume'
values indata
, with argumentssecondary_y='volume'
andtitle=ticker
.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Set the index of listings to Stock Symbol
listings_ss = listings.____(____)
# Get ticker of the largest Consumer Services company
ticker = listings_ss.____[____, ____].____()
# Set the start date
start = ____
# Import the stock data
data = ____
# Plot close and volume
data[['close', 'volume']].plot(secondary_y=____, title=____)
# Show the plot
plt.show()