ASH
ASH

Reputation: 20342

How can I run a for loop and append items to a list at the end of the loop?

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

Answers (1)

Trenton McKinney
Trenton McKinney

Reputation: 62483

  • The issue is, the code is probably making in-place updates to the dataframe prices.
    • This doesn't seem to be occurring in pandas version 1.1.1.
  • Each time the loop iterates, prices is changed
  • price_data = []
    • price_data = [prices, prices, prices]
  • To append the prices correctly, use .copy()
  • df = pd.concat(price_data) should not be in the loop
  • If you then want to perform a calculation per each ticker, use df.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

Related Questions