Carlo Silanu
Carlo Silanu

Reputation: 79

How to write a function that returns two variable from a dataframe

So I am currently working on a project from school.

This is my dataframe.

Div Date    Time    HomeTeam    AwayTeam    FTHG    FTAG    FTR HTHG    HTAG    ... AvgC<2.5    AHCh    B365CAHH    B365CAHA    PCAHH   PCAHA   MaxCAHH MaxCAHA AvgCAHH AvgCAHA
0   E0  09/08/2019  20:00   Liverpool   Norwich 4   1   H   4   0   ... 3.43    -2.25   1.91    1.99    1.94    1.98    1.99    2.07    1.90    1.99
1   E0  10/08/2019  12:30   West Ham    Man City    0   5   A   0   1   ... 2.91    1.75    1.95    1.95    1.96    1.97    2.07    1.98    1.97    1.92
2   E0  10/08/2019  15:00   Bournemouth Sheffield United    1   1   D   0   0   ... 1.92    -0.50   1.95    1.95    1.98    1.95    2.00    1.96    1.96    1.92
3   E0  10/08/2019  15:00   Burnley Southampton 3   0   H   0   0   ... 1.71    0.00    1.87    2.03    1.89    2.03    1.90    2.07    1.86    2.02
4   E0  10/08/2019  15:00   Crystal Palace  Everton 0   0   D   0   0   ... 1.71    0.25    1.82    2.08    1.97    1.96    2.03    2.08    1.96    1.93
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
283 E0  07/03/2020  15:00   Wolves  Brighton    0   0   D   0   0   ... 1.74    -0.75   1.98    1.92    2.01    1.92    2.03    1.99    1.94    1.93
284 E0  07/03/2020  17:30   Burnley Tottenham   1   1   D   1   0   ... 1.74    0.00    1.95    1.95    1.96    1.97    2.00    2.01    1.94    1.92
285 E0  08/03/2020  14:00   Chelsea Everton 4   0   H   2   0   ... 2.13    -0.50   1.85    2.05    1.88    2.04    2.00    2.11    1.86    2.01
286 E0  08/03/2020  16:30   Man United  Man City    2   0   H   1   0   ... 1.97    0.75    1.87    2.03    1.85    2.07    1.91    2.11    1.87    2.01
287 E0  09/03/2020  20:00   Leicester   Aston Villa 4   0   H   1   0   ... 2.28    -1.00   1.85    2.05    1.88    2.05    1.91    2.08    1.86    2.01

I am asked to write a function that takes one row and returns two variable, number of points form the home team and the away team from the column FTHG and FTAG.

The project also added

The rules of point calculations is the following: If two teams score the same number of goals by the end of the game, each receives 1 point. Otherwise, the team that scores more goals, receives 3 points, while the losing team receives 0 points.

I am struggling with this question, currently I can't think of a way to came up with this question. Can anybody help me?

Upvotes: 0

Views: 110

Answers (1)

FBruzzesi
FBruzzesi

Reputation: 6475

You can define a custom function and using with pandas.DataFrame.apply:

def give_point(row):

    if row['FTHG'] > row['FTAG']:
        row['PointsHG'] = 3
        row['PointsAG'] = 0

    elif row['FTHG'] < row['FTAG']:
        row['PointsHG'] = 0
        row['PointsAG'] = 3

    else:
        row['PointsHG'] = 1
        row['PointsAG'] = 1

    return row


df = df.apply(lambda row: give_point(row), axis=1)

Upvotes: 1

Related Questions