I have a pandas dataframe that looks as follows:
ID round player1 player2
1 1 A B
1 2 A C
1 3 B D
2 1 B C
2 2 C D
2 3 C E
3 1 B C
3 2 C D
3 3 C A
The dataframe contains sport match results, where the ID
column denotes one tournament, the round
column denotes the round for each tournament, and player1
and player2
columns contain the names of players that played against eachother in the respective round
.
I now want to cumulatively count the tournament participations for, say, player A
. In pseudocode this means: If the player with name A
comes up in either the player1
or player2
column per tournament ID
, increment the counter by 1.
The result should look like this (note: in my example player A
did participate in tournaments with the ID
s 1 and 3):
ID round player1 player2 playerAparticipated
1 1 A B 1
1 2 A C 1
1 3 B D 1
2 1 B C 0
2 2 C D 0
2 3 C E 0
3 1 B C 2
3 2 C D 2
3 3 C A 2
My current status is, that I added a "helper" column containing the values 1
or 0
denoting, if the respective player participated in the tournament:
ID round player1 player2 helper
1 1 A B 1
1 2 A C 1
1 3 B D 1
2 1 B C 0
2 2 C D 0
2 3 C E 0
3 1 B C 1
3 2 C D 1
3 3 C A 1
I think that I just need one final step, e.g., a smart use of cumsum()
that counts the helper
column in the desired way. However, I could not come up with the solution yet.