SP500_stocks = ["AAPL", "BA", "TSLA"]
SP_index = []
for stock in SP500_stocks:
s_data = pdr.get_data_yahoo(stock, startDate, endDate)
s_data = s_data["Adj Close"].pct_change()
SP_index.append(s_data)
df1 = pd.concat([SP_index[0], SP_index[1], SP_index[2]], axis = 1)
df1.dropna(inplace=True)`
In this code, I pull data from yahoo for the 3 tickers in SP500_stocks. The for loop returns a series I believe, 3 separate blocks of data, each having a date column and a percent return column. My question is, if I wanted to let's say have 51 stocks. I don't want to index SP_index 50 times. How do I write a for or while loop that does this? I was getting an error when trying to concatenate in the for loop above the df1 block. The df1 block allowed me to have the data side by side (aka 4 columns next to each other, 1st being the date column, followed by the 3 stocks returns).
Your code is generally fine. To simplify it, just use the following line to create the dataframe:
pd.concat(SP_index, axis = 1)
That way, you don't have to refer to each SP_index[i]
separately.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.