简体   繁体   中英

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

I have a data frame:

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       

using the following code I was able to find the colum header for the maximum value in that row:

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

How do I create another new column with Second_highest and Third_highest values? Desired output:

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

You can get all ranks at once using 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
 )

Output:

   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

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

one step for all rank

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM