我正在尝试计算FTSE100中每年每只股票的年收益。但是,使用嵌套循环后,我无法将计算出的值添加到新列中。
FTSE100 = pd.read_csv('FTSE100_joined_FULL.csv');
FTSE100
for x in range(FTSE100['Ticker'].nunique()):
stockno_arr = FTSE100['Ticker'].unique().tolist()
stock = FTSE100[FTSE100['Ticker'] == stockno_arr[x]]
stock["Date"] = pd.to_datetime(stock["Date"])
count = 0
for y in range (1900,2020):
if (stock[stock["Date"].dt.year == y].shape[0] != 0):
count += 1
stockx = stock[stock["Date"].dt.year == y]
FTSE100.loc[(FTSE100.Ticker == stockno_arr[x]) & (FTSE100.Date == y), 'Annual Returns'] = (((stockx['Close'] - stockx['Open'])/stockx['Open'] +1).product() -1)
错误似乎在代码的最后一行。这是我第一次问问题,所以我不确定如何附加图片或正确格式化我的问题。所以我想提前道歉。