1

Hi I have the following function to decide the winner:

def winner(T1,T2,S1,S2,PS1,PS2):
    if S1>S2:
        return T1        
    elif S2>S1:
        return T2
    else:
        #print('Winner will be decided via penalty shoot out')
        Ninit = 5
        Ts1 = np.sum(np.random.random(size=Ninit))*PS1
        Ts2 = np.sum(np.random.random(size=Ninit))*PS2
        if Ts1>Ts1:
            return T1
        elif Ts2>Ts1:
            return T2
        else:
            return 'Draw'

And I have the following data frame:

df = pd.DataFrame()
df['Team1']   = ['A','B','C','D','E','F']
df['Score1']  = [1,2,3,1,2,4]
df['Team2']   = ['U','V','W','X','Y','Z']
df['Score2']  = [2,2,2,2,3,3]
df['Match']   = df['Team1']  + ' Vs '+ df['Team2']
df['Match_no']= [1,2,3,4,5,6]
df ['P1'] = [0.8,0.7,0.6,0.9,0.75,0.77]
df ['P2'] = [0.75,0.75,0.65,0.78,0.79,0.85]

I want to create a new column in which winner from each match will be assigned. To decide a winner from each match, I used the function winner. I tested the function using arbitrary inputs. it works. When I used dataframe,

as follow:

df['Winner']= winner(df.Team1,df.Team2,df.Score1,df.Score2,df.P1,df.P2)

it showed me the following error:

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

Can anyone advise why there is an error? Thanks

Zep.

1

1 Answer 1

1

Your function isn't set up to take pandas.Series as inputs. Use a different way.

df['Winner'] = [
    winner(*t) for t in zip(df.Team1, df.Team2, df.Score1, df.Score2, df.P1, df.P2)]

df

  Team1  Score1 Team2  Score2   Match  Match_no    P1    P2 Winner
0     A       1     U       2  A Vs U         1  0.80  0.75      U
1     B       2     V       2  B Vs V         2  0.70  0.75      V
2     C       3     W       2  C Vs W         3  0.60  0.65      C
3     D       1     X       2  D Vs X         4  0.90  0.78      X
4     E       2     Y       3  E Vs Y         5  0.75  0.79      Y
5     F       4     Z       3  F Vs Z         6  0.77  0.85      F

Another way to go about it

def winner(T1,T2,S1,S2,PS1,PS2):
    ninit = 5
    Ts1 = np.random.rand(5).sum() * PS1
    Ts2 = np.random.rand(5).sum() * PS2
    a = np.select(
        [S1 > S2, S2 > S1, Ts1 > Ts2, Ts2 > Ts1],
        [T1, T2, T1, T2], 'DRAW')
    return a

df.assign(Winner=winner(df.Team1, df.Team2, df.Score1, df.Score2, df.P1, df.P2))

  Team1  Score1 Team2  Score2   Match  Match_no    P1    P2 Winner
0     A       1     U       2  A Vs U         1  0.80  0.75      U
1     B       2     V       2  B Vs V         2  0.70  0.75      B
2     C       3     W       2  C Vs W         3  0.60  0.65      C
3     D       1     X       2  D Vs X         4  0.90  0.78      X
4     E       2     Y       3  E Vs Y         5  0.75  0.79      Y
5     F       4     Z       3  F Vs Z         6  0.77  0.85      F
Sign up to request clarification or add additional context in comments.

2 Comments

Dear piRSquared, Thanks you so much for your advice. If you do not mind, would you advise me how to set up a function to take pandas series? Appreciate your help.
Thanks you so much piRSquared

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.