如果符合 python 中的条件,则查找并比较组中的最后一行和前一行

Find and compare the last and the row before in a group it if it matches the criteria in python

我有一个包含游戏数据的数据框。对于这个游戏,2 名玩家玩 90 分钟的游戏。然而,由于各种原因,游戏可以持续超过 90 分钟。我想要的是找到第 90 分钟后玩家 1 获胜的游戏。所以我想比较一场比赛的最终得分: - 持续超过 90 分钟, - 玩家 1 赢了 上次得分<90分钟


(来源:imggmi.com

  # Games dataframe
  games = pd.DataFrame({'game_id': {0: 1,1: 1,2: 1,3: 2,4: 2,5: 3,6: 3,7: 
                     3,8: 4,9: 4,10: 4,11: 5,12: 5,13: 5},
                    'time': {0: 1,1: 45,2: 95,3: 56,4: 80,5: 1,6: 95,7: 
                     95,8: 96,9: 107,10: 108,11: 15,12: 95,13: 97},
                    'player 1': {0: 1,1: 1,2: 2,3: 1,4: 1,5: 0,6: 1,7: 2,8: 
                     0,9: 1,10: 2,11: 1,12: 1,13: 1},
                    'player 2': {0: 0,1: 1,2: 1,3: 0,4: 1,5: 1,6: 1,7: 1,8: 
                     1,9: 1,10: 1,11: 0,12: 1,13: 2}})

  # Find the rows with the ending scores of games
  a = games.drop_duplicates(["game_id"],keep='last')

  #Find games that player 1 wins and time>90
  b = games[((games["player 1"] - games["player 2"]) >0) (games["time"]>90)]

例如,对于第 1 场比赛,玩家 1 在第 95 分钟获胜,在此之前比分是平的。总的来说,如果情况是第 90 分钟前 2 名玩家打平,或者玩家 1 输了,但在 90 分钟后,最终情况是玩家 1 赢了。我该如何过滤这种情况?

编辑: 这个怎么样?

endScores = games.loc[(games['time'] > 90) & (games['player 1'] > games['player 2'])].groupby('game_id').nth(-1)
beforeScores = games.loc[(games['time'] <= 90) & (games['player 1'] <= games['player 2'])].groupby('game_id').nth(-1)
compareGames = beforeScores.join(endScores, rsuffix='_end').dropna()