我有一个大的实验结果数据框架,我需要对其进行分类,以跨多个标准删除"占主导地位"的受试者。下面的"玩具"数据框反映了"实验"数据框的整体结构,但不一定是"实验"数据框的维度。
df = pd.DataFrame({'Subject': ['Alpha', 'Bravo', 'Charlie'],
'A': [6, 7, 8],
'B': [11, 7, 12],
'C': [13, 6, 6],
'D': [5, 9, 4],
'E': [11, 9, 5],
'F': [9, 10, 3],
'G': [2, 6, 5],
'H': [8, 12, 11]})
Subject A B C D E F G H
0 Alpha 6 11 13 5 11 9 2 8
1 Bravo 7 7 6 9 9 10 6 12
2 Charlie 8 12 6 4 5 3 5 11
如何使用"小于"两两比较生成以下结果?
[0, 1]: w=5, l=3, d=0
[0, 2]: w=4, l=4, d=0
[1, 2]: w=2, l=5, d=1
并将它们与以下伪代码组合以创建主导主题的子集['Bravo']并将其从原始数据框架中删除?
tx = 3
i = 0
subject[0]='Alpha'
subject[1]='Bravo'
if w > l and l < tx
then y[i] = subject[0]
z[i] = subject[1]
elseif w < l and w < tx
then y[i] = subject[1]
z[i] = subject[0]
i += 1
请通知吗?
下面的代码似乎可以正常工作
def pairwise_compare(dfq, pairs, tx):
winners = []
losers = []
for pair in pairs:
w = 0
l = 0
x = 0
for i in dfq['Subject']:
for j in dfq['Subject']:
if i == pair[0] and j == pair[1]:
alt_first = dfq.loc[dfq['Subject'] == i, 'A':'H'].values
alt_second = dfq.loc[dfq['Subject'] == j, 'A':'H'].values
diffs = (alt_first - alt_second).astype(int)
w = np.sum(diffs < 0)
l = np.sum(diffs > 0)
x = np.sum(diffs == 0)
if w > l and l < tx:
winners.append(i)
losers.append(j)
elif w < l and w < tx:
winners.append(j)
losers.append(i)
return winners, losers
pair_order_list = itertools.combinations(df['Subject'],2)
pairs = list(pair_order_list)
print('')
tx = 3
winners, losers = pairwise_compare(df, pairs, tx)
print('')
for winner, loser in zip(winners, losers):
df.drop(df[df['Subject'] == loser].index, inplace=True)
print(f'{loser} is dominated by {winner}')
df.set_index('Subject', inplace=True)
print('')
print(df)
并产生所需的输出。
Bravo is dominated by Charlie
A B C D E F G H
Subject
Alpha 6 11 13 5 11 9 2 8
Charlie 8 12 6 4 5 3 5 11
如果哪位"熊猫"专家能写出一个更地道的版本,我将不胜感激!