Comparing stocks to the S&P 500
The analyses until this point have been performed only between stocks. It is often useful to perform some of these against a market index such as the S&P 500. This will give a sense of how those stocks compare to movements in the overall market.
At the beginning of the chapter, we loaded the S&P 500 data for the same time period as the other stocks. To perform comparisons, we can perform the same calculations to derive the daily percentage change and cumulative returns on the index:
In [50]: sp_500_dpc = sp_500['Adj Close'].pct_change().fillna(0) sp_500_dpc[:5] Out[50]: Date 2012-01-03 0.000 2012-01-04 0.000 2012-01-05 0.003 2012-01-06 -0.003 2012-01-09 0.002 Name: Adj Close, dtype: float64
We can concatenate ...
Get Mastering pandas for Finance now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.