0

I would like to create a calculated column which is as shown in line 1 but is 0 otherwise

 openOptions['Cash Reserve'] =  (openOptions['Quantity'] * openOptions['Strike'] * 100)
 openOptions['Cash Reserve'] = 0 if (openOptions['OptionType'] == 'C') else openOptions['Cash Reserve']

I get an error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How should I resolve this?

1 Answer 1

1

You can use np.where(), which is like an if-then-else statement for numpy/Pandas, as follows:

openOptions['Cash Reserve'] = np.where(openOptions['OptionType'] == 'C',
                                       0,
                                       openOptions['Cash Reserve'])

Alternatively, as your Cash Reserve column has already been set up and you modify it to 0 only under a condition, you can also use .loc[] :

openOptions.loc[openOptions['OptionType'] == 'C', 'Cash Reserve'] = 0

Or even further simplified, as follows:

openOptions['Cash Reserve'][openOptions['OptionType'] == 'C'] = 0
Sign up to request clarification or add additional context in comments.

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.