Import index component price information
Now you'll use the stock symbols for the companies you selected in the last exercise to calculate returns for each company.
This exercise is part of the course
Manipulating Time Series Data in Python
Exercise instructions
We have already imported pandas
as pd
and matplotlib.pyplot
as plt
for you. We have also made the variable tickers
available to you, which contains the Stock Symbol
for each index component as a list
.
- Print
tickers
to verify the content matches your result from the last exercise. - Use
pd.read_csv()
to import'stock_prices.csv'
, parsing the'Date'
column and also setting the'Date'
column as index before assigning the result tostock_prices
. Inspect the result using.info()
. - Calculate the price return for the index components by dividing the last row of
stock_prices
by the first, subtracting 1 and multiplying by 100. Assign the result toprice_return
. - Plot a horizontal bar chart of the sorted returns with the title
Stock Price Returns
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Print tickers
print(____)
# Import prices and inspect result
stock_prices = ____
print(____)
# Calculate the returns
price_return = ____
# Plot horizontal bar chart of sorted price_return