[英]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.
在下面的示例中,我循环遍历了一个名为“tickers”的列表 3 次。 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.
我想在每个循环结束时将 append 一些分析的结果添加到一个列表中,因此追加三个,而不是 100+ 个追加。 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.现在的情况是,我得到了 dataframe 中的所有项目作为最后一个自动收报机,但前两个都不见了。 I want the ticker + str(new_seriesdata['Adj Close'].iloc[-1]), which is the last item in the dataframe.
我想要 ticker + str(new_seriesdata['Adj Close'].iloc[-1]),这是 dataframe 中的最后一项。
prices
.prices
进行就地更新。
prices
is changedprices
都会改变price_data = []
price_data = [prices, prices, prices]
prices
correctly, use .copy()
prices
正确,请使用.copy()
df = pd.concat(price_data)
should not be in the loop df = pd.concat(price_data)
不应该在循环中df.groupby('ticker')
and aggregate the calculation.df.groupby('ticker')
并聚合计算。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
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.
在下面的示例中,我在列表调用“tickers”中循环了 3 次。 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.
我想在每个循环的末尾将一些分析的结果附加到一个列表中,所以三个附加,而不是 100+ 个附加。 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.
我想要股票代码 + str(new_seriesdata['Adj Close'].iloc[-1]),它是数据框中的最后一项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.