我想合并两个具有完全相同列名的 DataFrame。重叠的列可以添加在一起。我遇到了一些麻烦,因为分组应该发生在名为“日期”的“索引”上,但我无法通过使用“日期”名称来创建该索引。
实际上,我只需要指数(日期)和所有股票的“Adj Close”总和。
我试过:
data.join(temp, how='outer')
返回: “ValueError:列重叠但未指定后缀:Index(['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'], dtype='object')”
data = pd.concat([data, temp]).groupby([data.index, temp.index], as_index=True).sum(axis=1)
返回: “石斑鱼和轴的长度必须相同
data = pd.merge(data, temp, left_index=True, right_index=True)['Adj Close'].sum(axis=1, skipna=True).astype(np.int64)
返回: “KeyError:'调整关闭'”
代码
def overview(request):
stocks = Stock.objects.all()
data = None
for stock in stocks:
if data is None:
data = yf.download(stock.ticker, start=stock.trade_date, period="ytd")
else:
temp = yf.download(stock.ticker, start=stock.trade_date, period="ytd")
data.join(temp, how='outer')
数据帧输出 1
[*********************100%***********************] 1 of 1 completed
Open High ... Adj Close Volume
Date ...
2019-09-19 55.502499 55.939999 ... 54.697304 88242400
2019-09-20 55.345001 55.639999 ... 53.897728 221652400
2019-09-23 54.737499 54.959999 ... 54.142803 76662000
2019-09-24 55.257500 55.622501 ... 53.885353 124763200
2019-09-25 54.637501 55.375000 ... 54.714626 87613600
... ... ... ... ... ...
2020-09-10 120.360001 120.500000 ... 113.489998 182274400
2020-09-11 114.570000 115.230003 ... 112.000000 180860300
2020-09-14 114.720001 115.930000 ... 115.360001 140150100
2020-09-15 118.330002 118.830002 ... 115.540001 184642000
2020-09-16 115.230003 116.000000 ... 112.129997 154679000
[251 rows x 6 columns]
慕婉清6462132
相关分类