我有这样的数据帧大熊猫GROUPBY值在不同的列
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