Dataframe Column Comparison Raises Valueerror: The Truth Value Of A Series Is Ambiguous.
I'm trying to compare two columns to see if one value is larger than the other, but I keep getting a ValueError: ValueError: The truth value of a Series is ambiguous. Use a.empty,
Solution 1:
Here's how you reproduce this error:
df = pd.DataFrame({'Roll Price': np.random.randint(1, 10, 10),
'Delta VWAP': np.random.randint(1, 10, 10)})
df
Out:
Delta VWAP Roll Price
0 7 6
1 9 1
2 9 4
3 2 4
4 7 8
5 8 4
6 8 6
7 9 3
8 2 5
9 6 8
ifdf['Roll Price'] > df['Delta VWAP']:
df['Result'] = 'Long'
Traceback (most recent call last):
File "<ipython-input-18-a07b1f06bd42>", line 1, in <module>
ifdf['Roll Price'] > df['Delta VWAP']:
File "/home/ayhan/anaconda3/lib/python3.5/site-packages/pandas/core/generic.py", line 955, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
The error stems from this comparison: df['Roll Price'] > df['Delta VWAP']
If you execute this
df['Roll Price'] > df['Delta VWAP']
Out:
0False1False2False3True4True5False6False7False8True9True
dtype: bool
You see that the result is not a single True
or False
value but instead an array of booleans. And the ambiguous part is
- Do you want to set the column to
Long
when all of the values in the array are True? - Do you want to set the column to
Long
when any of the values in the array is True?
It turns out the answer is neither. You want to do element-wise comparison and set the corresponding value to Long
when the condition is satisfied, and to Short
otherwise.
For that, you can use np.where
:
cond = df['Roll Price'] > df['Delta VWAP']
df['Result'] = np.where(cond, 'Long', 'Short')
df
Out:
Delta VWAP Roll Price Result
076Short191Short294Short324Long478Long584Short686Short793Short825Long968Long
Post a Comment for "Dataframe Column Comparison Raises Valueerror: The Truth Value Of A Series Is Ambiguous."