可以保存计算值在数据框架中导致计算值
我正在构建一个数据框架,其中包含来自Yahoo Finance的交易数据。
当我尝试计算每个股票的循环返回时,只有最后一个结果保存在数据框架中,而其余的股票则为NAN。
我认为,除最后一张股票外,每个股票都覆盖了计算的回报,但不知道发生这种情况的原因是什么。
这是我的代码:
- 来自Yahoo Finance的加载数据:
trading_df = pd.DataFrame()
for stock in test_stocks:
st = web.DataReader(stock, 'yahoo', start, end)
st['Symbol'] = stock
trading_df = trading_df.append(st)
- 计算返回:
for stock in test_stocks:
trading_df['Return'] = trading_df[trading_df['Symbol'] == stock]['Adj Close'].pct_change(1)
- 结果:
预先感谢。
I'm building a dataframe which contains trading data from Yahoo Finance.
When I'm trying to compute Return using for loop for each stock, only the last result is saved in dataframe, while rest of the stocks are Nan.
I assume that computed returns are overwrite for every stock except the last one, but have no idea what is the reason for that occurrence.
Here's my code:
- Load data from Yahoo Finance:
trading_df = pd.DataFrame()
for stock in test_stocks:
st = web.DataReader(stock, 'yahoo', start, end)
st['Symbol'] = stock
trading_df = trading_df.append(st)
- Computing Return:
for stock in test_stocks:
trading_df['Return'] = trading_df[trading_df['Symbol'] == stock]['Adj Close'].pct_change(1)
- The result:
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
分配值时,您需要切片框架
You need to slice the dataframe when assigning values