- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个包含以下列的数据框:
| 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/
您好,我正在处理 BIRT 报告。我有一个查询,我必须对父级的重复数据进行分组,但子级也不能分组! 在我的查询中: item 是父项,item_ledger_entry 是子项。我有来自 item.N
我正在使用 GA API。 这是针对 MCF 目标报告(底部)的标准目标完成指标表(顶部) 看一下这个: 总数加起来 (12,238),但看看按 channel 分组的分割有多么不同!我以为这些会很接
我正在开发一个流量计数器,我想获得 IP 和重复计数,但是如何? 就像是 :select ip, count(ip) from Redirect 返回 : null total ip count 重定
我尝试编写一个正则表达式来匹配条件表达式,例如: a!=2 1+2=2+a 我尝试提取运算符。我当前的正则表达式是“.+([!=<>]+).+” 但问题是匹配器总是尝试匹配组中可能的最短字符串
在 MS Transact SQL 中,假设我有一个这样的表(订单): Order Date Order Total Customer # 09/30/2008 8
我想按 m.ID 分组,并对每个 m.id 求和 (pm.amount_construction* prod.anzahl) 实际上我有以下结果: Meterial_id | amount_const
我想根据多列中的值对值进行分组。这是一个例子: 我想得到输出: {{-30,-50,20},{-20,30,60},{-30,NULL or other value, 20}} 我设法到达: SELE
我正在尝试找出运行此查询的最佳方式。我基本上需要返回在我们的系统中只下了一个订单的客户的“登录”字段列表(登录字段基本上是客户 ID/ key )。 我们系统的一些背景...... 客户在同一日期下的
给定以下mysql结果集: id code name importance '1234', 'ID-CS-B', 'Chocolate Sauce'
大家好,我的数据框中有以下列: LC_REF 1 DT 16 2C 2 DT 16 2C 3 DT 16 2C 1 DT 16 3C 6 DT 16 3C 3
我有这样的 mongoDB 集合 { "_id" : "EkKTRrpH4FY9AuRLj", "stage" : 10, }, { "_id" : "EkKTRrpH4FY9
假设我有一组数据对,其中 index 0 是值,index 1 是类型: input = [ ('11013331', 'KAT'), ('9085267',
java中用stream进行去重,排序,分组 一、distinct 1. 八大基本数据类型 List collect = ListUtil.of(1, 2, 3, 1, 2).stream().fil
基本上,我从 TABLE_A 中的这个开始 France - 100 France - 200 France - 300 Mexico - 50 Mexico - 50 Mexico - 56 Pol
我希望这个正则表达式 ([A-Z]+)$ 将选择此示例中的最后一次出现: AB.012.00.022ABC-1 AB.013.00.022AB-1 AB.014.00.022ABAB-1 但我没有匹配
我创建了一个数据透视表,但数据没有组合在一起。 任何人都可以帮助我获得所需的格式吗? 我为获取数据透视表而编写的查询: DECLARE @cols AS NVARCHAR(MAX), -- f
我想按时间段(月,周,日,小时,...)选择计数和分组。例如,我想选择行数并将它们按 24 小时分组。 我的表创建如下。日期是时间戳。 CREATE TABLE MSG ( MSG_ID dec
在 SQL Server 2005 中,我有一个包含如下数据的表: WTN------------Date 555-111-1212 2009-01-01 555-111-1212 2009-
题 假设我有 k 个标量列,如果它们沿着每列彼此在一定距离内,我想对它们进行分组。 假设简单 k 是 2 并且它们是我唯一的列。 pd.DataFrame(list(zip(sorted(choice
问题 在以下数据框中 df : import random import pandas as pd random.seed(999) sz = 50 qty = {'one': 1, 'two': 2
我是一名优秀的程序员,十分优秀!