如何分析/比较熊猫中所有行的成对组合并保持计数?

如何解决如何分析/比较熊猫中所有行的成对组合并保持计数?

我的df.shape (15,4)的{​​{1}}

df.head()

我想成对比较df中的所有行。 (A1,A2)之间的比较; (A1,A3); (A1,A4); (A1,A4); (A2,A3); (A2,A4); (A2,A5); (A3,A4)..依此类推,并带有相应的列。

比较标准: C1 C2 C3 C4 A1 82.0 78.00 1100 3.0 A2 19.0 99.00 9520 3.0 A3 25.0 42.00 1700 7.0 A4 93.0 37.00 1700 7.0 A5 9.2 0.44 510 7.0

if(A1,C1 > A2,C1)--> then keep count of A1 as winner and A2 as loser

将所有这些成对行比较的所有计数存储在新的df中。

对于给定的elif(A1,C1 < A2,C1)--> vice versa A2 has a winner count,A1 gets loser count elif(A1,C1 == A2,C1)--> Add 0.5 to the count of both A1 and A2 since it is a draw./ or can keep count in a new Draw count column. 输出应如下所示:

df.head()

到目前为止,我已经能够生成具有所有可能组合的df:

df_new.head()

     Wins  Loses  Draws  
A1     7    8       1 
A2     6    9       1 
A3     8    5       3 
A4     9    4       3 
A5     2    12      2 

(值的变化) 如何从这些所有可能的组合中提取获胜计数?

解决方法

虽然这不是很好的解决方案,但它可以工作。我相信有更有效的方法来解决这个问题。

import pandas as pd

# note that this function will compare the row with itself
# thus producing one additional draw
def outcome(x,a):
    return (x<a,x==a,x>a)
 
# placeholder columns,C2 is unused
d = {'C1': [2.0,1.0,4.0,9.0,5.0,3.0,2.0],'C2': [1,2,3,4,5,6,7,8,9]}
# named index
ind = ['A'+str(x+1) for x in range(len(d['C1']))]
# construct dataframe
df = pd.DataFrame(data=d,index=ind)


res = []
#iterate over
for index,row in df.iterrows():
    # count wins,draws,defeats
    res.append((index,df['C1'].apply(lambda x: outcome(x,row['C1']))))

# parse results to obtain clean sums of outcomes
ind = [str(res[j][0]) for j in range(len(res))]
res = [[sum([y[i] for y in res[j][1]]) for i in range(3)] for j in range(len(res))]

# create temporary dataframe
tempDf = pd.DataFrame(res,columns=['W','D','L'],index = ind)
# correct draw counts
tempDf['D']-=1

# merge dataframes
df = df.merge(tempDf,left_index=True,right_index=True)

输出为

     C1  C2  W  D  L
A1  2.0   1  2  1  5
A2  1.0   2  0  1  7
A3  1.0   3  0  1  7
A4  4.0   4  5  0  3
A5  9.0   5  8  0  0
A6  5.0   6  6  1  1
A7  5.0   7  6  1  1
A8  3.0   8  4  0  4
A9  2.0   9  2  1  5

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?