samedi 30 novembre 2019

Python (2.7) - Simple IF THEN logic considering multiple fields

Attempting to perform something I can do in 5 second in Excel in Python and I'm finding myself very frustrated.

I have data that looks like this:

        HomeTeam AwayTeam  HomeScore  AwayScore  TotalScore
0        OAK      LAC         26         24          50
1        CHI      DET         20         13          33
2        CIN      BAL         13         49          62
3        CLE      BUF         19         16          35
4         NO      ATL          9         26          35

I want to create a NEW COLUMN in the data frame called "WINNER". If HomeScore > AwayScore, I want the new column WINNER to equal the HomeTeam value, ELSE, equal the AwayTeam value. For example,WINNER should = OAK on row 0 and CHI on row 1.

this is what I have attempted so far, but this did not give me the results needed above.

I know this is simple! Please help

#new df
df1 = df[['HomeTeam','AwayTeam','HomeScore','AwayScore','TotalScore']]

df1['winner'] = lambda x : df['HomeTeam'] if (df['HomeScore'] > df['AwayScore']) else df['AwayTeam']

print(df1.head())

Aucun commentaire:

Enregistrer un commentaire