gpt4 book ai didi

python - Pandas - 胜率计算;按两列分组并计数

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:16 24 4
gpt4 key购买 nike

我有一个包含以下列的数据框:

| winner |  loser  | tournament |
+--------+---------+------------+
| John | Steve | A |
+--------+---------+------------+
| Steve | John | B |
+--------+---------+------------+
| John | Michael | A |
+--------+---------+------------+
| Steve | John | A |
+--------+---------+------------+

我想做的是针对给定的锦标赛类型计算获胜者和失败者的历史获胜百分比,并将其放在自己的列中。


填写上表的示例如下。游戏将被称为(赢家,输家,类型)。

我还添加了中间计算列以使其更清晰。


1) 对于第一场比赛(约翰、史蒂夫、A)。之前没有类型A的游戏,所以填0。

2) 第二场比赛(史蒂夫、约翰、B)。之前没有B类游戏,所以填0。

3) 第三场比赛(约翰、迈克尔、A)。之前有Type A的比赛,所以我们可以得到信息。首先,约翰是赢家。他在积分榜第一排赢了一场A类比赛。所以我们把 winner wins = 1。John 之前没有输过 A 类游戏,所以我们把 winner losses = 0。Michael 没有任何游戏历史,所以我们填充 loser wins = 0 和 losers losses = 0。

4) 第 4 场比赛,(Steve, John, A)。我们看到史蒂夫之前没有赢过任何类型 A 的比赛,所以我们将获胜者获胜次数设为 0。他输掉了 1 场类型 A 的比赛(第一行)。所以我们把获胜者的损失 = 1。约翰赢了 2 场 A 类比赛,所以失败者赢了 = 2。他输了

    +--------+---------+------------+-------------+------------+---------------+--------------+--------------+-------------+
| winner | loser | tournament | winner wins | loser wins | winner losses | loser losses | winner win % | loser win % |
+--------+---------+------------+-------------+------------+---------------+--------------+--------------+-------------+
| John | Steve | A | 0 | 0 | 0 | 0 | 0/(0+0) | 0/(0+0) |
+--------+---------+------------+-------------+------------+---------------+--------------+--------------+-------------+
| Steve | John | B | 0 | 0 | 0 | 0 | 0/(0+0) | 0/(0+0) |
+--------+---------+------------+-------------+------------+---------------+--------------+--------------+-------------+
| John | Michael | A | 1 | 0 | 0 | 0 | 1/(1+0) | 0/(0+0) |
+--------+---------+------------+-------------+------------+---------------+--------------+--------------+-------------+
| Steve | John | A | 0 | 2 | 1 | 0 | 0/(0+1) | 2/(2+0) |
+--------+---------+------------+-------------+------------+---------------+--------------+--------------+-------------

最佳答案

这应该会产生预期的结果:

def win_los_percent(sdf):
sdf['winner wins'] = sdf.groupby('winner').cumcount()
sdf['winner losses'] = [(sdf.loc[0:i, 'loser'] == sdf.loc[i, 'winner']).sum() for i in sdf.index]
sdf['loser losses'] = sdf.groupby('loser').cumcount()
sdf['loser wins'] = [(sdf.loc[0:i, 'winner'] == sdf.loc[i, 'loser']).sum() for i in sdf.index]
sdf['winner win %'] = sdf['winner wins'] / (sdf['winner wins'] + sdf['winner losses'])
sdf['loser win %'] = sdf['loser wins'] / (sdf['loser wins'] + sdf['loser losses'])
return sdf

ddf = df.groupby('tournament').apply(win_los_percent)

使用提供的数据,ddf 是:

  winner    loser tournament  winner wins  winner losses  loser losses  loser wins  winner win %  loser win %
0 John Steve A 0 0 0 0 NaN NaN
1 Steve John B 0 0 0 0 NaN NaN
2 John Michael A 1 0 0 0 1.0 NaN
3 Steve John A 0 1 0 2 0.0 1.0

pandas groupby用于对同一锦标赛的数据进行分组,并将子数据帧传递给 win_los_percent 函数。返回此函数的返回值以构建最终数据框。

对于每个子集,该函数计算几列:

  • sdf['winner wins']sdf['loser losses'] 是通过使用cumcount 获得的:对于每一行,此方法计算分组列中值(玩家名称)的先前出现次数。
  • 获取 sdf['winner losses']sdf['loser wins'] 稍微复杂一些,因为我们需要计算一个值之前出现的次数(玩家姓名)在另一列。
    列表理解遍历数据帧索引以选择前面的行并检查 'winner' 列中的玩家名称是否等于 loser 列中当前行的玩家名称> 反之亦然。 sum() 允许计算 True 值:True 转换为 1,False 转换为 0,总和给出想要的结果:玩家名称在前几行中出现了多少次。
  • 百分比列是通过矢量化获得的。结果为 NaN 的地方是因为除以 0。

关于python - Pandas - 胜率计算;按两列分组并计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58027733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com