0

I'm trying to compare the values of two columns with the following code:

if df['predicted_spread'] > df['vegas_spread']:
total_bet += 1

It is returning the following error:

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

Any idea how I can fix this?

user3294779
  • 593
  • 2
  • 7
  • 23
  • Take a look at this: http://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – wwl Oct 25 '16 at 23:21
  • You should check your logic firstly, what do you want to achieve? `df['predicted_spread'] > df['vegas_spread']` returns a logic series while if expects a logic value. – Psidom Oct 25 '16 at 23:21

1 Answers1

4

First, the result of df['predicted_spread'] > df['vegas_spread'] is also a Series consist of boolean value, e.g., [True, True, False, ...], so you get error message The truth value of a Series is ambiguous.

So what should you do? That's depended on your application.

(1) if your intention is to count the number of True condition, then

 total_bet = sum(df['predicted_spread'] > df['vegas_spread'])

(2) if your intention is to increase total_bet when all comparation of df['predicted_spead'] > df['vegas_spread'] is true, then

if (df['predicted_spread'] > df['vegas_spread']).all() is True:
    total_bet += 1

(3) if total_bet is also a vector, and your intention is to record each comparation, then

total_bet = total_bet + (df['predicted_spread'] > df['vegas_spread'])

I guess option (1) is what you need. Thanks.

rojeeer
  • 1,991
  • 1
  • 11
  • 13