Skip to content Skip to sidebar Skip to footer

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

if df['Roll Price'] > df['Delta VWAP']:
    df['Result'] = 'Long'

Traceback (most recent call last):

  File "<ipython-input-18-a07b1f06bd42>", line 1, in <module>
    if df['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: 
0    False
1    False
2    False
3     True
4     True
5    False
6    False
7    False
8     True
9     True
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
0           7           6  Short
1           9           1  Short
2           9           4  Short
3           2           4   Long
4           7           8   Long
5           8           4  Short
6           8           6  Short
7           9           3  Short
8           2           5   Long
9           6           8   Long

Post a Comment for "DataFrame Column Comparison Raises ValueError: The Truth Value Of A Series Is Ambiguous."