我有这个数据框
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})
通过这样做,我可以保持获胜者获胜的总次数。
frame['winners_wins'] = frame.groupby('winner')['ones'].cumsum()
我想保持对 player1 的输赢和对 player2 的连续计数。我想我应该可以使用 groupby 函数来做到这一点,但我不知道如何编写它。
编辑:
我第一次说的不是很好。我想跟踪每个单独的玩家。所以所需的输出将是:
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