The following program generates a return matrix with three stocks plus S&P500:
import statsimport numpy as np import pandas as pd tickers=['IBM','dell','wmt'] path1='http://chart.yahoo.com/table.csv?s=^GSPC' final=pd.read_csv(path1,usecols=[0,6],index_col=0) final.columns=['^GSPC'] path2='http://chart.yahoo.com/table.csv?s=ttt' for ticker in tickers: print ticker x = pd.read_csv(path2.replace('ttt',ticker),usecols=[0,6],index_col=0) x.columns=[ticker] final=pd.merge(final,x,left_index=True,right_index=True)
To show the first and last few lines, we use the .head()
and .tail()
functions as follows:
>>>final.head() ^GSPC IBM dell wmt Date 2013-10-18 1744.50 172.85 13.83 75.71 2013-10-17 1733.15 173.90 13.85 75.78 2013-10-16 1721.54 185.73 13.85 75.60 2013-10-15 1698.06 183.67 13.83 74.37 2013-10-14 1710.14 185.97 13.85 74.68 >>>final.tail() ...