0
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))

Gives an error: ValueError: Wrong number of items passed 2963, placement implies 1

I just want to do the following: if df.oc is a positive number, then df.uptail = (df.high - df.close) ...else df.uptail = (df.high - df.open)

How can I crank this out?

2
  • Your lambda doesn't make use of it's parameter... Commented Mar 8, 2019 at 17:46
  • I tried on a phone but got lost in the logic. You probably want np.where but it's not clear exactly how it works for your example. Commented Mar 8, 2019 at 17:47

2 Answers 2

1

np.where is more suited here.

df['uptail'] = np.where(df.close-df.open>=0, df.high-df.close, df.high-df.open)
Sign up to request clarification or add additional context in comments.

1 Comment

For brevity: df.close >= df.open equals df.close - df.open >= 0
0

It looks like you want uptail to be close - open if that's positive, and high - open if close - open is negative or 0.

You can achieve that with the following code:

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.