简体   繁体   中英

Comparing two columns in pandas

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?

First, the result of df['predicted_spread'] > df['vegas_spread'] is also a Series consist of boolean value, eg, [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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM