I've got a DataFrame
which contains stock values.
It looks like this:
>>>Data Open High Low Close Volume Adj Close Date
2013-07-08 76.91 77.81 76.85 77.04 5106200 77.04
When I try to make a conditional new column with the following if statement:
Data['Test'] =Data['Close'] if Data['Close'] > Data['Open'] else Data['Open']
I get the following error:
Traceback (most recent call last):
File "<pyshell#116>", line 1, in <module>
Data[1]['Test'] =Data[1]['Close'] if Data[1]['Close'] > Data[1]['Open'] else Data[1]['Open']
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I then used a.all()
:
Data[1]['Test'] =Data[1]['Close'] if all(Data[1]['Close'] > Data[1]['Open']) else Data[1]['Open']
The result was that the entire ['Open']
Column was selected. I didn't get the condition that I wanted, which is to select every time the biggest value between the ['Open']
and ['Close']
columns.
Any help is appreciated.
Thanks.