I am trying to get a cumulative sum using groupby where the cumulative sum is applied to multiple columns that contain the same value
import pandas as pd
import numpy as np
df = pd.DataFrame([['Jazz', 'Clippers', 89, 100],
['Clippers' , 'Jazz', 101, 97],
['Bucks' , 'Jazz', 99, 112],
['Jazz' , 'Bucks', 109, 88]],
columns=['home_team', 'away_team', 'home_points', 'away_points'])
print(df)
This will produce a dataframe with output of
home_team away_team home_points away_points
0 Jazz Clippers 89 100
1 Clippers Jazz 101 97
2 Bucks Jazz 99 112
3 Jazz Bucks 109 88
what I'm trying to do is get the cumulative total points for the home and away team that will account for the fact that each team appears in both home and away columns but all I have been able to figure out is the cumulative total grouped by team name that totals each team as home OR away, like so
df["home_cumulative_points"]= df.groupby(["home_team"])["home_points"].cumsum()
df["away_cumulative_points"]= df.groupby(["away_team"])["away_points"].cumsum()
print(df)
which produces
home_team away_team home_points away_points home_cumulative_points away_cumulative_points
0 Jazz Clippers 89 100 89 100
1 Clippers Jazz 101 97 101 97
2 Bucks Jazz 99 112 99 209
3 Jazz Bucks 109 88 198 88
Is there any way I can groupby to make the cumulative sum account for the presence of the same team in the home and away column to make the running sum add the teams points regardless of if they were home or away? So the ideal output of the last row would be
home_team away_team home_points away_points home_cumulative_points away_cumulative_points
3 Jazz Bucks 109 88 407 187
I'm guessing I may need to do a for loop or something but I'm just not sure how best to go about it. Thanks in advance for any feedback!