0

I have a database where four of its columns are: score_home, score_away, home_id and away_id.

I expect to get a variable whose rows contain the winning ID in each game.

Index gRes
0 GB
1 GB

For that, I tried with the following code

team_f['gRes'] = 0
if team_f['score_home'] > team_f['score_away']:
    team_f['gRes'] = team_f['home_id']
else:
    team_f['gRes'] = team_f['away_id']

and i get the following error

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

Could you suggest how to correct the error or, failing that, any alternatives to build the variable?

2 Answers2

0

This is ambiguous because of how you compare the two columns.

Try np.where

team_f['gRes'] = np.where(team_f['score_home'] > team_f['score_away'], team_f["home_id"], team_f["away_id"])

Please include a minimal example of your data if that does not work.

Edit: If draws are possible, np.select is preferred. (See Naveed's answer)

bitflip
  • 3,436
  • 1
  • 3
  • 22
0

what if the score are tied? you'll have three conditions, and np.Select helps

# define a list of conditions, and corresponding list of values, 
# when condition is true, 
# and default value as a third attribute

df['gRes'] = np.select([team_f['score_home'] > team_f['score_away'], #cond 1
                       team_f['score_home'] < team_f['score_away']   #cond 2
                       ],
                       [team_f['home_id'],  # cond1 true
                        team_f['away_id']], # cond2 true
                       'drawn'              # default, game tied
                      )

PS: if you share a data(preferably as a code) or text, I'd be able to share the result

Naveed
  • 11,495
  • 2
  • 14
  • 21