how do I change the result column based on the conditions of 2 columns?

Viewed 46

database I tried the following but it didnt work. I am relatively new in pandas, any help is appriciated.

if(lambda x:df['HomeGoals']>df['AwayGoals']):
    df['Result']='H'
elif(lambda x:df['HomeGoals']<df['AwayGoals']):
    df['Result']='A'
else:
    df['Result']='D'
2 Answers

Let us try something new np.sign

df['R']=np.sign(df['HomeGoals']-df['AwayGoals']).map({1:'H',0:'D',-1:'A'})

If you want to separate into a function

def calculate(reg):
    if reg['HomeGoals'] > reg['AwayGoals']:
        return 'H'
    elif reg['HomeGoals'] < reg['AwayGoals']:
        return 'A' 
    else:
        return 'D'

df['Result'] = df.apply(calculate, axis=1)
Related