df = pd.read_csv('data/eurusd_dukascopy.csv')
df.columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume']
df['oc'] = df.close - df.open
df['uptail'] = df['oc'].apply(lambda x: (df.high - df.close) if x >= 0 else (df.high - df.open))
给出错误:ValueError:通过2963的项目数错误,放置意味着1
我只想执行以下操作:如果df.oc是正数,则df.uptail =(df.high - df.close)...否则df.uptail =(df.high - df.open)
怎么能把它搞砸了?
np.where更适合这里。
df['uptail'] = np.where(df.close-df.open>=0, df.high-df.close, df.high-df.open)
看起来你想要uptail
是close - open
,如果那是积极的,high - open
如果close - open
是负数或0。
您可以使用以下代码实现此目的:
df['uptail'] = df.high - df.open
df.loc[df.close > df.open, 'uptail'] = \
df.loc[df.close > df.open, 'high'] - df.loc[df.close > df.open, 'close']