查找数据帧一行中第二大值的列 header

Finding column header for the second highest value in a row of a dataframe

我有一个数据框:

Patch_0  Patch_1  Patch_2   Patch_7  exp_patch  
0        0.0      70.0      70.0       3            
1        0.0      70.0      74.0       4       

使用以下代码,我能够找到该行中最大值的列 header:

df3['Highest_Rew_patch'] = df3.max(axis=1)
s = df3.iloc[:, df3.columns.str.startswith('Patch')].apply(lambda s:s.index[s.eq(s.max())].tolist(), axis=1)
s = s.apply(lambda s: ','.join(s))

Patch_0  Patch_1  Patch_2   Patch_7  exp_patch  Highest_Rew_patch
0        0.0      70.0      70.0       3            Patch_2,Patch_7
1        0.0      70.0      74.0       4            Patch_7

如何使用 Second_highest 和 Third_highest 值创建另一个新列? 期望的输出:

Patch_0     Patch_1    Patch_2   Patch_7   exp_patch     Highest_Rew_patch    Second_highest        
    0.0        0.0      70.0      70.0       3            Patch_2,Patch_7      Patch_0,Patch_1
    1.0        0.0      70.0      74.0       4            Patch_7              Patch_2

保留您的代码,但不使用 s.max(),而是使用 s.nlargest(2)[-1]

所有等级一步

s = pd.wide_to_long(df.reset_index(),['Patch'],i = 'index',j='a',suffix='\w+',sep='_').reset_index()
s['new'] = s.groupby('index')['Patch'].rank('dense')

out = df.join(pd.crosstab(index=s['index'],columns = s['new'], values= s['a'].astype(str).radd('Patch'),aggfunc=','.join))

您可以使用rank and reshaping with melt and pivot_table一次获得所有等级:

df.join(df.filter(like='Patch')
   .rank(method='dense', ascending=False, axis=1)
   .astype(int)
   .reset_index()
   .melt('index')
   .pivot_table(index='index', columns='value', values='variable', aggfunc=','.join)
 .iloc[:,:3] # optional: keep only top 3
 )

输出:

   Patch_0  Patch_1  Patch_2  Patch_7  exp_patch                1                2        3
0        0      0.0     70.0     70.0          3  Patch_2,Patch_7  Patch_0,Patch_1      NaN
1        1      0.0     70.0     74.0          4          Patch_7          Patch_2  Patch_0