从 pandas 的所有组合中找到获胜者(更多)
Finding winner (greater number) out of all combinations in pandas
所以我的fd.shape (105,4)
fd.head()
Ccom Wcom Lcom Dcom
(A1, A2) (82.0, 19.0) (78.0, 99.0) (1100.0, 9520.0) (3.0, 3.0)
(A1, A3) (82.0, 25.0) (78.0, 42.0) (1100.0, 1700.0) (3.0, 7.0)
(A1, A4) (82.0, 93.0) (78.0, 37.0) (1100.0, 1700.0) (3.0, 7.0)
(A1, A5) (82.0, 9.2) (78.0, 0.44) (1100.0, 510.0) (3.0, 7.0)
(A1, A6) (82.0, 52.0) (78.0, 0.042) (1100.0, 1100.0) (3.0, 17.2)
具有 df 的所有可能组合。现在我想从每个 cell/combination 中找到获胜者(更大的数字)并计算该数字的获胜次数。
输出应该是这样的:
Wins
A1 34
A2 44
A3 53
A4 76 .... and so on till last row
A15 43
这个新数据框基本上记录了 A 大于其组合的次数。
如果值是元组已经跳过这一步。如果值是字符串,则将它们转换为元组
import ast
df = df.reset_index().applymap(ast.literal_eval).set_index('index')
现在比较值并从索引中的元组中提取列。输出不是您预期的输出,因为您的示例输入数据不是您的整个数据框。
import numpy as np
(df.applymap(lambda x: np.sign(x[1] - x[0]))
.apply(pd.Series.value_counts, axis=1).drop(0.0, 1)
.apply(lambda x: pd.Series(x.values, index=x.name), axis=1)
.reset_index(drop=True).T.sum(1).astype('int')
)
输出:
A1 9
A2 2
A3 2
A4 3
A5 1
A6 1
dtype: int64
重现此解决方案
使用值作为元组设置数据框
import pandas as pd
import io
import ast
a = """
Ccom Wcom Lcom Dcom
('A1', 'A2') (82.0, 19.0) (78.0, 99.0) (1100.0, 9520.0) (3.0, 3.0)
('A1', 'A3') (82.0, 25.0) (78.0, 42.0) (1100.0, 1700.0) (3.0, 7.0)
('A1', 'A4') (82.0, 93.0) (78.0, 37.0) (1100.0, 1700.0) (3.0, 7.0)
('A1', 'A5') (82.0, 9.2) (78.0, 0.44) (1100.0, 510.0) (3.0, 7.0)
('A1', 'A6') (82.0, 52.0) (78.0, 0.042) (1100.0, 1100.0) (3.0, 17.2)"""
df = pd.read_csv(io.StringIO(a), sep='\s\s+', engine='python')
df = df.reset_index().applymap(ast.literal_eval).set_index('index')
输出:
Ccom Wcom Lcom Dcom
index
(A1, A2) (82.0, 19.0) (78.0, 99.0) (1100.0, 9520.0) (3.0, 3.0)
(A1, A3) (82.0, 25.0) (78.0, 42.0) (1100.0, 1700.0) (3.0, 7.0)
(A1, A4) (82.0, 93.0) (78.0, 37.0) (1100.0, 1700.0) (3.0, 7.0)
(A1, A5) (82.0, 9.2) (78.0, 0.44) (1100.0, 510.0) (3.0, 7.0)
(A1, A6) (82.0, 52.0) (78.0, 0.042) (1100.0, 1100.0) (3.0, 17.2)
计算胜利和平局
为避免计算两次,解决方案略有不同。要计算两位玩家的平局,您需要两个值。这就是为什么使用 df_counts[[0,0]]
.
选择两次列 0.0
的原因
df_counts = (df.applymap(lambda x: np.sign(x[1] - x[0]))
.apply(pd.Series.value_counts, axis=1))
# column 'wins'
df_result = (df_counts[[-1,1]].apply(lambda x: pd.Series(x.values, index=x.name), axis=1)
.reset_index(drop=True).T.sum(1).astype('int').to_frame('wins'))
# column 'draws'
df_result['draws'] = (df_counts[[0,0]].apply(lambda x: pd.Series(x.values, index=x.name), axis=1)
.reset_index(drop=True).T.sum(1).astype('int'))
df_result
输出:
wins draws
A1 9 2
A2 2 1
A3 2 0
A4 3 0
A5 1 0
A6 1 1
所以我的fd.shape (105,4)
fd.head()
Ccom Wcom Lcom Dcom
(A1, A2) (82.0, 19.0) (78.0, 99.0) (1100.0, 9520.0) (3.0, 3.0)
(A1, A3) (82.0, 25.0) (78.0, 42.0) (1100.0, 1700.0) (3.0, 7.0)
(A1, A4) (82.0, 93.0) (78.0, 37.0) (1100.0, 1700.0) (3.0, 7.0)
(A1, A5) (82.0, 9.2) (78.0, 0.44) (1100.0, 510.0) (3.0, 7.0)
(A1, A6) (82.0, 52.0) (78.0, 0.042) (1100.0, 1100.0) (3.0, 17.2)
具有 df 的所有可能组合。现在我想从每个 cell/combination 中找到获胜者(更大的数字)并计算该数字的获胜次数。 输出应该是这样的:
Wins
A1 34
A2 44
A3 53
A4 76 .... and so on till last row
A15 43
这个新数据框基本上记录了 A 大于其组合的次数。
如果值是元组已经跳过这一步。如果值是字符串,则将它们转换为元组
import ast
df = df.reset_index().applymap(ast.literal_eval).set_index('index')
现在比较值并从索引中的元组中提取列。输出不是您预期的输出,因为您的示例输入数据不是您的整个数据框。
import numpy as np
(df.applymap(lambda x: np.sign(x[1] - x[0]))
.apply(pd.Series.value_counts, axis=1).drop(0.0, 1)
.apply(lambda x: pd.Series(x.values, index=x.name), axis=1)
.reset_index(drop=True).T.sum(1).astype('int')
)
输出:
A1 9
A2 2
A3 2
A4 3
A5 1
A6 1
dtype: int64
重现此解决方案
使用值作为元组设置数据框
import pandas as pd
import io
import ast
a = """
Ccom Wcom Lcom Dcom
('A1', 'A2') (82.0, 19.0) (78.0, 99.0) (1100.0, 9520.0) (3.0, 3.0)
('A1', 'A3') (82.0, 25.0) (78.0, 42.0) (1100.0, 1700.0) (3.0, 7.0)
('A1', 'A4') (82.0, 93.0) (78.0, 37.0) (1100.0, 1700.0) (3.0, 7.0)
('A1', 'A5') (82.0, 9.2) (78.0, 0.44) (1100.0, 510.0) (3.0, 7.0)
('A1', 'A6') (82.0, 52.0) (78.0, 0.042) (1100.0, 1100.0) (3.0, 17.2)"""
df = pd.read_csv(io.StringIO(a), sep='\s\s+', engine='python')
df = df.reset_index().applymap(ast.literal_eval).set_index('index')
输出:
Ccom Wcom Lcom Dcom
index
(A1, A2) (82.0, 19.0) (78.0, 99.0) (1100.0, 9520.0) (3.0, 3.0)
(A1, A3) (82.0, 25.0) (78.0, 42.0) (1100.0, 1700.0) (3.0, 7.0)
(A1, A4) (82.0, 93.0) (78.0, 37.0) (1100.0, 1700.0) (3.0, 7.0)
(A1, A5) (82.0, 9.2) (78.0, 0.44) (1100.0, 510.0) (3.0, 7.0)
(A1, A6) (82.0, 52.0) (78.0, 0.042) (1100.0, 1100.0) (3.0, 17.2)
计算胜利和平局
为避免计算两次,解决方案略有不同。要计算两位玩家的平局,您需要两个值。这就是为什么使用 df_counts[[0,0]]
.
0.0
的原因
df_counts = (df.applymap(lambda x: np.sign(x[1] - x[0]))
.apply(pd.Series.value_counts, axis=1))
# column 'wins'
df_result = (df_counts[[-1,1]].apply(lambda x: pd.Series(x.values, index=x.name), axis=1)
.reset_index(drop=True).T.sum(1).astype('int').to_frame('wins'))
# column 'draws'
df_result['draws'] = (df_counts[[0,0]].apply(lambda x: pd.Series(x.values, index=x.name), axis=1)
.reset_index(drop=True).T.sum(1).astype('int'))
df_result
输出:
wins draws
A1 9 2
A2 2 1
A3 2 0
A4 3 0
A5 1 0
A6 1 1