Session Ready
Exercise

Compare annual stock price trends

In the video, you have seen how to select sub-periods from a time series.

You'll use this to compare the performance for three years of Yahoo stock prices.

Instructions
100 XP

We have already imported pandas as pd and matplotlib.pyplot as plt and we have already loaded the 'yahoo.csv' file in a variable yahoo with DateTimeIndex and a single column price.

  • Create an empty pd.DataFrame() called prices.
  • Iterate over a list containing the three years, 2013, 2014, and 2015, as string, and in each loop:
    • Use the iteration variable to select the data for this year and the column price.
    • Use .reset_index() with drop=True to remove the DatetimeIndex.
    • Rename the column price column to the appropriate year.
    • Use pd.concat() to combine the yearly data with the data in prices along axis=1.
  • Plot prices.