Reputation: 2918
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?
Upvotes: 0
Views: 47
Reputation: 23217
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
Upvotes: 1