Python numpy中的条件句。如何使用pandas或numpy将3个或更多的数据放入数据帧?
我的语法有问题。我想在LastPrice>到较低波段时买入,在LastPrice==sma级别时卖出,如果这是真的,我想将结果放在一列中,如:“买入”与“卖出”不同 我的代码:Python numpy中的条件句。如何使用pandas或numpy将3个或更多的数据放入数据帧?,python,pandas,numpy,Python,Pandas,Numpy,我的语法有问题。我想在LastPrice>到较低波段时买入,在LastPrice==sma级别时卖出,如果这是真的,我想将结果放在一列中,如:“买入”与“卖出”不同 我的代码: df['LastPrice'].dropna(inplace=True) sma = df['LastPrice'].rolling(window=20).mean() rstd = df['LastPrice'].rolling(window=20).std() df['upper_band'] = sma + 2 *
df['LastPrice'].dropna(inplace=True)
sma = df['LastPrice'].rolling(window=20).mean()
rstd = df['LastPrice'].rolling(window=20).std()
df['upper_band'] = sma + 2 * rstd
df['lower_band'] = sma - 2 * rstd
df['laseñalota'] = np.where((df['LastPrice'] > df['lower_band'],"Buy") & (df['LastPrice'] == sma), "Sell")
错误是:
operands could not be broadcast together with shapes (2,) (4508,)
您不需要链接
numpy。在这里调用的地方,只有两个选项。如果要有两个以上的选项,请使用numpy。选择
df['laseñalota'] = np.where(df['LastPrice'] > df['lower_band'], 'Buy',
np.where(df['LastPrice'] <= sma, 'Sell', 'Do Nothing'))
condlist = [df['LastPrice'] > df['lower_band'], df['LastPrice'] <= sma]
choicelist = ['Buy', 'Sell']
df['new_laseñalota'] = np.select(condlist, choicelist)