Can somebody help me add a new column to a pandas dataframe. I have a dataframe containing football games over several seasons, the rows in the dataframe are: kick_of_date, season, team1_name, team2_name, team1_goals, team2_goals. In the new columns, I want one average goal so far that season (should be based on the date) for each team, and also one column for each team containing average goals scored last season. The average goals should be both when the team is playing home and away. All teams play at least one time home and exist in df['team1_name'].unique(). find_start_of_season(date) is a function returning the last august based on the kick-off date.
My best guess is this for so far this season:
df['team1_avg_goals_this_season'] = [[df.between_time(find_start_of_season(j), j)[df['team1_name'] == i]['team1_goals']+[df.between_time(find_start_of_season(j), j)[df['team2_name'] == i]['team2_goals']].mean() for i in df['team1_name'].unique()] for j in df['kick_off_datetime'].unique()]
and regarding average last season, my best guess is:
df['team1_avg_goals_last_season'] = [[df[(((df['team1_name'] == i) & (df['season'] == j))]['declared_inj_time'].mean() for i in df['team1_name'].unique()] for j in df['season'].unique()]
These are not correct and I am hoping anybody knows a better solution. An example of is provided(snippet):
| kick_off_date | season | team1_name | team2_name | team1_goals | team2_goals |
|---|---|---|---|---|---|
| 10/8/2018 | 2018-2019 | Man United | Leicester | 2 | 1 |
| 11/8/2018 | 2018-2019 | Bournemouth | Cardiff | 2 | 0 |
| 11/8/2018 | 2018-2019 | Fulham | Crystal Palace | 0 | 2 |
| 11/8/2018 | 2018-2019 | Huddersfield | Chelsea | 0 | 3 |
| 11/8/2018 | 2018-2019 | Newcastle | Tottenham | 1 | 2 |
| 11/8/2018 | 2018-2019 | Watford | Brighton | 2 | 0 |
| 11/8/2018 | 2018-2019 | Wolves | Everton | 2 | 2 |
| 12/8/2018 | 2018-2019 | Arsenal | Man City | 0 | 2 |
| 12/8/2018 | 2018-2019 | Liverpool | West Ham | 4 | 0 |
| 12/8/2018 | 2018-2019 | Southampton | Burnley | 0 | 0 |
| 18/8/2018 | 2018-2019 | Cardiff | Newcastle | 0 | 0 |
| 18/8/2018 | 2018-2019 | Chelsea | Arsenal | 3 | 2 |
| 18/8/2018 | 2018-2019 | Everton | Southampton | 2 | 1 |
| 18/8/2018 | 2018-2019 | Leicester | Wolves | 2 | 0 |
| 18/8/2018 | 2018-2019 | Tottenham | Fulham | 3 | 1 |
| 18/8/2018 | 2018-2019 | West Ham | Bournemouth | 1 | 2 |
| 19/8/2018 | 2018-2019 | Brighton | Man United | 3 | 2 |
| 19/8/2018 | 2018-2019 | Burnley | Watford | 1 | 3 |
| 19/8/2018 | 2018-2019 | Man City | Huddersfield | 6 | 1 |
| 20/8/2018 | 2018-2019 | Crystal Palace | Liverpool | 0 | 2 |
| 25/8/2018 | 2018-2019 | Arsenal | West Ham | 3 | 1 |
| 25/8/2018 | 2018-2019 | Bournemouth | Everton | 2 | 2 |
| 25/8/2018 | 2018-2019 | Huddersfield | Cardiff | 0 | 0 |
| 25/8/2018 | 2018-2019 | Liverpool | Brighton | 1 | 0 |
| 25/8/2018 | 2018-2019 | Southampton | Leicester | 1 | 2 |
| 25/8/2018 | 2018-2019 | Wolves | Man City | 1 | 1 |
Including the new columns:
| kick_off_date | season | team1_name | team2_name | team1_goals | team2_goals | team1_avg_goals_this_season | team2_avg_goals_this_season | team1_avg_goals_last_season | team2_avg_goals_last_season |
|---|---|---|---|---|---|---|---|---|---|
| 10/8/2018 | 2018-2019 | Man United | Leicester | 2 | 1 | 0 | 0 | 2.64 | 1.89 |
| 11/8/2018 | 2018-2019 | Bournemouth | Cardiff | 2 | 0 | 0 | 0 | 1.45 | 1.32 |
| 11/8/2018 | 2018-2019 | Fulham | Crystal Palace | 0 | 2 | 0 | 0 | 1.35 | 1.71 |
| 11/8/2018 | 2018-2019 | Huddersfield | Chelsea | 0 | 3 | 0 | 0 | 1.50 | 2.22 |
| 11/8/2018 | 2018-2019 | Newcastle | Tottenham | 1 | 2 | 0 | 0 | 1.91 | 2.14 |
| 11/8/2018 | 2018-2019 | Watford | Brighton | 2 | 0 | 0 | 0 | 1.47 | 1.96 |
| 11/8/2018 | 2018-2019 | Wolves | Everton | 2 | 2 | 0 | 0 | 2.06 | 2.13 |
| 12/8/2018 | 2018-2019 | Arsenal | Man City | 0 | 2 | 0 | 0 | 2.55 | 2.71 |
| 12/8/2018 | 2018-2019 | Liverpool | West Ham | 4 | 0 | 0 | 0 | 2.35 | 1.39 |
| 12/8/2018 | 2018-2019 | Southampton | Burnley | 0 | 0 | 0 | 0 | 1.88 | 1.26 |
| 18/8/2018 | 2018-2019 | Cardiff | Newcastle | 0 | 0 | 0 | 0 | 1.32 | 1.91 |
| 18/8/2018 | 2018-2019 | Chelsea | Arsenal | 3 | 2 | 3 | 0 | 2.22 | 2.55 |
| 18/8/2018 | 2018-2019 | Everton | Southampton | 2 | 1 | 2 | 0 | 2.13 | 1.88 |
| 18/8/2018 | 2018-2019 | Leicester | Wolves | 2 | 0 | 2 | 2 | 1.89 | 2.06 |
| 18/8/2018 | 2018-2019 | Tottenham | Fulham | 3 | 1 | 2 | 0 | 2.14 | 1.35 |
| 18/8/2018 | 2018-2019 | West Ham | Bournemouth | 1 | 2 | 0 | 2 | 1.39 | 1.45 |
| 19/8/2018 | 2018-2019 | Brighton | Man United | 3 | 2 | 0 | 2 | 1.96 | 2.64 |
| 19/8/2018 | 2018-2019 | Burnley | Watford | 1 | 3 | 0 | 2 | 1.26 | 1.47 |
| 19/8/2018 | 2018-2019 | Man City | Huddersfield | 6 | 1 | 2 | 0 | 2.71 | 1.50 |
| 20/8/2018 | 2018-2019 | Crystal Palace | Liverpool | 0 | 2 | 2 | 4 | 1.71 | 2.35 |
| 25/8/2018 | 2018-2019 | Arsenal | West Ham | 3 | 1 | 1 | 0.5 | 2.55 | 1.39 |
| 25/8/2018 | 2018-2019 | Bournemouth | Everton | 2 | 2 | 2 | 2 | 1.45 | 2.13 |
| 25/8/2018 | 2018-2019 | Huddersfield | Cardiff | 0 | 0 | 0.5 | 0 | 1.50 | 1.32 |
| 25/8/2018 | 2018-2019 | Liverpool | Brighton | 1 | 0 | 3 | 1.5 | 2.35 | 1.96 |
| 25/8/2018 | 2018-2019 | Southampton | Leicester | 1 | 2 | 0.5 | 1.5 | 1.88 | 1.89 |
| 25/8/2018 | 2018-2019 | Wolves | Man City | 1 | 1 | 1 | 4 | 2.06 | 2.71 |
Behold these tables are handwritten and errors may occur. Also, the average last season numbers are made up.
Hopefully, anybody has had an equal problem and knows how to alternate my code to work. Hope to hear from anybody!