Python Forum
Pandas - cumulative sum of two columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas - cumulative sum of two columns
#8
I know it's ugly and I'm not experienced with pandas but it works:p

import pandas as pd

def get_team_names(df):
    return df[0][0], df[1][0]


df  = pd.DataFrame([
["Gothenburg", "Malmo", 2018,"A"],
["Malmo","Gothenburg",  2018, "D"],
["Malmo", "Gothenburg", 2018, "A"],
["Gothenburg", "Malmo", 2018, "H"],
["Gothenburg", "Malmo" ,2018, "D"],
["Gothenburg", "Malmo", 2018, "A"],
["Gothenburg", "Malmo", 2018, "H"],
["Malmo", "Gothenburg", 2018, "A"],
["Gothenburg", "Malmo", 2018, "H"],
["Malmo", "Gothenburg", 2018, "H"],
[ "Malmo","Gothenburg", 2018, "D"],
[ "Malmo", "Gothenburg",2018, "H"],
 
])

team_1, team_2 = get_team_names(df)

df.columns = ['H_team', 'A_team', 'Year', 'Result']

draws = df['Result']=='D'

hh = (df['Result']=='H').where(df['H_team'] == team_1) * 3
ha = (df['Result']=='A').where(df['A_team'] == team_1) * 3

ah = (df['Result']=='H').where(df['H_team'] == team_2) * 3
aa = (df['Result']=='A').where(df['A_team'] == team_2) * 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'] = team_1_points.where(df['H_team'] == team_1, team_2_points)
df['A_points'] = team_1_points.where(df['A_team'] == team_1, team_2_points)

print(df)
Output:
H_team A_team Year Result H_points A_points 0 Gothenburg Malmo 2018 A NaN NaN 1 Malmo Gothenburg 2018 D 3.0 0.0 2 Malmo Gothenburg 2018 A 4.0 1.0 3 Gothenburg Malmo 2018 H 4.0 4.0 4 Gothenburg Malmo 2018 D 7.0 4.0 5 Gothenburg Malmo 2018 A 8.0 5.0 6 Gothenburg Malmo 2018 H 8.0 8.0 7 Malmo Gothenburg 2018 A 8.0 11.0 8 Gothenburg Malmo 2018 H 14.0 8.0 9 Malmo Gothenburg 2018 H 8.0 17.0 10 Malmo Gothenburg 2018 D 11.0 17.0 11 Malmo Gothenburg 2018 H 12.0 18.0
Reply


Messages In This Thread
Pandas - cumulative sum of two columns - by tobbs - May-19-2019, 09:29 AM
RE: Pandas - cumulative sum of two columns - by michalmonday - May-24-2019, 10:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 3,338 May-09-2021, 06:46 PM
Last Post: Pit292
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 2,480 Feb-20-2021, 08:25 PM
Last Post: perfringo
  Remove extra count columns created by pandas groupby spyf8 1 2,752 Feb-10-2021, 09:19 AM
Last Post: Naheed
  Pandas: summing columns conditional on the column labels ddd2332 0 2,157 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Difference of two columns in Pandas dataframe zinho 2 3,398 Jun-17-2020, 03:36 PM
Last Post: zinho
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 4,804 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  [pandas] How to re-arrange DataFrame columns SriMekala 8 4,977 Jun-22-2019, 12:55 AM
Last Post: scidam
  comparing two columns two different files in pandas nuncio 0 2,425 Jun-06-2018, 01:04 PM
Last Post: nuncio
  subtruction of columns in pandas garikhgh0 3 2,688 May-14-2018, 10:31 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020