I have this data frame
frame = pd.DataFrame({'player1' : ['Joe', 'Steve', 'Bill', 'Doug', 'Steve','Bill','Joe','Steve'],
'player2' : ['Bill', 'Doug', 'Steve', 'Joe', 'Bill', 'Steve', 'Doug', 'Bill'],
'winner' : ['Joe','Steve' , 'Steve','Doug', 'Bill', 'Steve', 'Doug', 'Steve'],
'loser' : ['Bill', 'Doug', 'Bill', 'Joe', 'Steve', 'Bill', 'Joe', 'Bill'],
'ones' : 1})
I can keep a running total of how many times the winner has won by doing this.
frame['winners_wins'] = frame.groupby('winner')['ones'].cumsum()
I would like to keep a running count of player1's wins and losses and the same for player2. I think I should be able to do this with the groupby function but I don't know how to write it.
edit:
I didn't say it very the well the first time. I would like to keep track for each individual player. So the desired output would be:
player1 player2 winner loser player1_wins player2_wins
Joe Bill Joe Bill 1 0
Steve Doug Steve Doug 1 0
Bill Steve Steve Bill 0 2
Doug Joe Doug Joe 1 1
Steve Bill Bill Steve 2 1
Bill Steve Steve Bill 1 3
Joe Doug Doug Joe 1 2
Steve Bill Steve Bill 3 1