vendredi 11 janvier 2019

Create list with sequential counter of each respective team's wins

Say I have a dataset that contains the home_team, away_team and columns home_win, away_win that tells which team won the game. Like this:

Home_team     Away_Team     Home_Win     Away_Win    gameID
   TB            CLB            1            0         1
   NY            ARZ            0            1         2
   EDM           CAN            1            0         3
   NY            TB             0            1         4
   NY            CLB            1            0         5
   TB            NY             1            0         6

How do you write a sequential counter that counts a Teams Total Wins with respect for previous games and irrespective if the Team was Home or Away. So for gameID:1, each team has a total of 0 total wins. Since TB won the first game they now have a total of 1 wins coming up to their second game agains NY(gameID:4) and NY has a total of 0 previous wins.

So the data would look like this: (AT=Away_Team, HT=Home_Team)

Home_team     Away_Team     Home_Win     Away_Win    gameID    HT'sTotWins      AT'sTotWins
   TB            CLB            1            0         1            0               0
   NY            ARZ            0            1         2            0               0
   EDM           CAN            1            0         3            0               0
   NY            TB             0            1         4            0               1
   NY            CLB            1            0         5            0               0
   TB            NY             1            0         6            2               1

I've read some about GroupBy.cumcount(), but I don't know how to write the conditions. I hope I'm not to unclear about what I want to do, if I am please tell me.

Aucun commentaire:

Enregistrer un commentaire