Ken H. Dizon
Ken H. Dizon

Reputation: 31

Goals for and Goals Against I want to return result (Win, Draw, Loss). Pandas Python

Trying to create a new column "Results" based on GF and GA. Wondering how I would do this via defining functions and condition statements. Below is my initial attempt for one row but can't figure out how I would apply to all rows, greatly appreciate your assistance!

Date Venue Opponent GF GA
08-19-2017 H Crystal Palace 1 0
08-27-2017 H Arsenal FC 4 0
09-16-2017 H Burnley FC 1 1
10-14-2017 H Manchester United 0 0
10-28-2017 H Huddersfield Town 3 0

Raw input:

df = pd.DataFrame({'Date': ['08-19-2017', '08-27-2017', '09-16-2017', '10-14-2017', '10-28-2017'],
                   'Venue': ['H', 'H', 'H', 'H', 'H'],
                   'Opponent': ['Crystal Palace', 'Arsenal FC', 'Burnley FC', 'Manchester United', 'Huddersfield Town'],
                   'GF': [1, 4, 1, 0, 3],
                   'GA': [0, 0, 1, 0, 0]})

My attempt:

    # for one row (match)
gf = df[0, 3]
ga = df[0, 4]
        
if gf > ga:
    print('W')
elif gf < ga:
    print('L')
else:
    print('D')
    

output: W

Would to apply this to all rows, how would I go about a solution based on:

 ```# create a function
def win_draw_loss():
    
if df['GF'] > df['GA']:
    print('W')
elif mo_rec['GF'].sum() < mo_rec['GA'].sum():
    print('L')
else:
    print('D')

# making the new pandas column
df['results'] =  ```

Upvotes: 3

Views: 200

Answers (1)

user7864386
user7864386

Reputation:

You can correct your win_draw_loss function a little bit and apply it to each row of your DataFrame df. The issue with your function that is corrected below is that win_draw_loss will have to take row data as arguments if you want to apply it to the DataFrame, so your evaluation also needs to be on row data.

def win_draw_loss(x):
    
    if x['GF'] > x['GA']:
        return 'W'
    elif x['GF'] < x['GA']:
        return 'L'
    else:
        return 'D'

df['Results'] = df.apply(win_draw_loss, axis=1)

You can also use np.select:

import numpy as np
df['Results'] = np.select([df['GF'] > df['GA'], df['GF'] < df['GA']], ['W', 'L'], 'D')

Output:

        Date Venue           Opponent  GF  GA  Results
0  08-19-2017     H     Crystal Palace   1   0       W
1  08-27-2017     H         Arsenal FC   4   0       W
2  09-16-2017     H         Burnley FC   1   1       D
3  10-14-2017     H  Manchester United   0   0       D
4  10-28-2017     H  Huddersfield Town   3   0       W

Upvotes: 4

Related Questions