1. Learn
  2. /
  3. Courses
  4. /
  5. Manipulating Time Series Data in Python

Exercise

Plot performance difference vs benchmark index

In the video, you learned how to calculate and plot the performance difference of a stock in percentage points relative to a benchmark index.

Let's compare the performance of Microsoft (MSFT) and Apple (AAPL) to the S&P 500 over the last 10 years.

Instructions

100 XP

We have already imported pandas as pd and matplotlib.pyplot as plt.

  • Create the list tickers containing the two stock symbols.
  • Use pd.read_csv() to import 'msft_aapl.csv' and 'sp500.csv', creating a DatetimeIndex for each from the 'date' column using parse_dates and index_col, and assign the result to stocks and sp500, respectively.
  • Use pd.concat() to concatenate stocks and sp500 along axis=1, apply .dropna() to drop all missing values, and assign the result to data.
  • Normalize data by dividing by the first price, multiply by 100 and assign the output to normalized.
  • Select tickers from normalized, and subtract normalized['SP500'] with keyword axis=0 to align the indexes, then plot the result.