May-25-2019, 10:25 AM
Glad I could help, btw I think it would be important to make sure that the 'Year' value actually groups games from particular season, not strictly a year.
import pandas as pd def get_team_names(df): return df[0][0], df[1][0] df = pd.DataFrame([ ["Gothenburg", "Malmo", 2010,"A"], ["Malmo","Gothenburg", 2010, "D"], ["Malmo", "Gothenburg", 2010, "A"], ["Gothenburg", "Malmo", 2010, "H"], ["Gothenburg", "Malmo" ,2011, "D"], ["Gothenburg", "Malmo", 2011, "A"], ["Gothenburg", "Malmo", 2011, "H"], ["Malmo", "Gothenburg", 2011, "A"], ["Gothenburg", "Malmo", 2012, "H"], ["Malmo", "Gothenburg", 2012, "H"], [ "Malmo","Gothenburg", 2012, "D"], [ "Malmo", "Gothenburg",2012, "H"], ["Malmo", "Gothenburg", 2013, "H"], [ "Malmo","Gothenburg", 2013, "D"], [ "Malmo", "Gothenburg",2013, "H"], ]) team_1, team_2 = get_team_names(df) df.columns = ['H_team', 'A_team', 'Year', 'Result'] df['H_points'] = 0 df['A_points'] = 0 for year in set(df['Year']): draws = (df['Result']=='D').where(df['Year'] == year) hh = (df['Result']=='H').where((df['H_team'] == team_1) & (df['Year'] == year)) * 3 ha = (df['Result']=='A').where((df['A_team'] == team_1) & (df['Year'] == year)) * 3 ah = (df['Result']=='H').where((df['H_team'] == team_2) & (df['Year'] == year)) * 3 aa = (df['Result']=='A').where((df['A_team'] == team_2) & (df['Year'] == year)) * 3 team_1_points = hh.add(ha, fill_value=0).add(draws, fill_value=0).cumsum().shift(1) team_2_points = ah.add(aa, fill_value=0).add(draws, fill_value=0).cumsum().shift(1) df['H_points'] = df['H_points'].add( team_1_points.where(df['H_team'] == team_1, team_2_points), fill_value=0) df['A_points'] = df['A_points'].add( team_1_points.where(df['A_team'] == team_1, team_2_points), fill_value=0) # set first game of season to 0 points - https://stackoverflow.com/a/37911231/4620679 # otherwise it would be equal to the sum of points from previous season (due to ".shift(1)" method) df.loc[df.index[df['Year'] == year][:1], 'H_points'] = 0 df.loc[df.index[df['Year'] == year][:1], 'A_points'] = 0 print(df)
Output: H_team A_team Year Result H_points A_points
0 Gothenburg Malmo 2010 A 0.0 0.0
1 Malmo Gothenburg 2010 D 3.0 0.0
2 Malmo Gothenburg 2010 A 4.0 1.0
3 Gothenburg Malmo 2010 H 4.0 4.0
4 Gothenburg Malmo 2011 D 0.0 0.0
5 Gothenburg Malmo 2011 A 1.0 1.0
6 Gothenburg Malmo 2011 H 1.0 4.0
7 Malmo Gothenburg 2011 A 4.0 4.0
8 Gothenburg Malmo 2012 H 0.0 0.0
9 Malmo Gothenburg 2012 H 0.0 3.0
10 Malmo Gothenburg 2012 D 3.0 3.0
11 Malmo Gothenburg 2012 H 4.0 4.0
12 Malmo Gothenburg 2013 H 0.0 0.0
13 Malmo Gothenburg 2013 D 3.0 0.0
14 Malmo Gothenburg 2013 H 4.0 1.0