从 yahoo Finance 下载 ohlcv for nvidia 读取,我正在为信号购买/dontbuy 创建一个列,当我尝试定义哪些通过 avg>volume 测试时,一切要么全部“购买”,要么不购买。df=pd.read_csv('NVDA.csv',dtype={'label':str})df['Price%delta']=((df['Close']/df['Open'])*100) df['Avg_volume']=df['Volume'].rolling(7).mean()df['Signal']=0for index, row in df.iterrows(): if row['Volume'] > row['Avg_volume']: df['Signal']='Buy' else: df['Signal']='Dont Buy'
3 回答
慕斯709654
TA贡献1840条经验 获得超5个赞
你根本不需要 for 循环:
mask = df["Volume"] > df["Avg_volume"]
df.loc[mask, "Signal"] = "Buy"
df.loc[~mask, "Signal"] = 'Don't buy'
当年话下
TA贡献1890条经验 获得超9个赞
使用np.where()
以下方法的矢量化解决方案:
df['Signal'] = np.where(df['Volume'] > df['Avg_volume'], 'Buy', 'Dont Buy')
慕工程0101907
TA贡献1887条经验 获得超5个赞
您没有指定任何索引来分配'Buy'或'Don't buy'。使用loc来代替:
for index, row in df.iterrows():
if row['Volume'] > row['Avg_volume']:
df.loc[index, 'Signal']='Buy'
else:
df.loc[index, 'Signal']='Dont Buy'
添加回答
举报
0/150
提交
取消