Reputation: 20342
I am missing something pretty basic here, but it's been a long day, and I'm not sure what is off. In the example below, I am looping through a list call 'tickers', three times. I want to append the results of some analysis to a list at the end of each loop, so three appends, rather than 100+ appends. Here is my code.
from pandas_datareader import data as wb
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pylab import rcParams
from sklearn.preprocessing import MinMaxScaler
start = '2020-03-01'
end = '2020-09-22'
tickers = ['TAN','QCLN','PBW']
thelen = len(tickers)
z=0
all_stocks=[]
price_data = []
for ticker in tickers:
prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Open','Adj Close']]
price_data.append(prices.assign(ticker=ticker)[['ticker', 'Open', 'Adj Close']])
#names = np.reshape(price_data, (len(price_data), 1))
df = pd.concat(price_data)
df.reset_index(inplace=True)
# doing some analysis in here, then writing results to a dataframe...
z=z+1
print(str(z) + ' of ' + str(thelen))
all_stocks.append(ticker + ' act: ' + str(new_seriesdata['Adj Close'].iloc[-1]) + ' prd: ' + str(myclosing_priceresult))
The way it is now, I get all the items in the dataframe for the last ticker, but the first two are gone. I want the ticker + str(new_seriesdata['Adj Close'].iloc[-1]), which is the last item in the dataframe.
Upvotes: 0
Views: 119
Reputation: 62483
prices
.
prices
is changedprice_data = []
price_data = [prices, prices, prices]
prices
correctly, use .copy()
df = pd.concat(price_data)
should not be in the loopdf.groupby('ticker')
and aggregate the calculation.price_data = []
for ticker in tickers:
prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Open','Adj Close']]
price_data.append(prices.assign(ticker=ticker)[['ticker', 'Open', 'Adj Close']].copy())
df = pd.concat(price_data).reset_index()
df.head()
Date ticker Open Adj Close
0 2020-03-02 TAN 36.630001 36.990002
1 2020-03-03 TAN 37.770000 37.130001
2 2020-03-04 TAN 38.130001 38.520000
3 2020-03-05 TAN 37.639999 38.330002
4 2020-03-06 TAN 37.299999 36.880001
df.tail()
Date ticker Open Adj Close
424 2020-09-16 PBW 57.410000 57.650002
425 2020-09-17 PBW 56.130001 56.480000
426 2020-09-18 PBW 57.189999 57.310001
427 2020-09-21 PBW 56.139999 56.639999
428 2020-09-22 PBW 56.580002 56.509998
Upvotes: 2