简体   繁体   English

Python-熊猫:找出对角线以外的最大值

[英]Python - Pandas: Find largests values off-diagonal

This is somewhat complicated. 这有点复杂。 I have multiple dataframes df , all with the same columns and index that looks like the one below: 我有多个数据框df ,都具有相同的列和索引,如下所示:

       E          F          H            
row                                                                     
CE     17.917153 10.875160   9.970251 
CF     9.780500  16.261098  10.021619    
CH     12.293967 10.608844  10.870527 

My objective in each df is to find the two maximum values in the dataframe and their corresponding index and column . 我在每个df目标是在数据帧及其对应的indexcolumn找到两个最大值。 These max values cannot be on the diagonal . 这些最大值不能在对角线上 How can I do so? 我该怎么办?

from pandas import *


L = [[17.917153, 10.875160,   9.970251],   
     [9.780500,  16.261098,  10.021619], 
     [12.293967, 10.608844,  10.870527]]

df = DataFrame(L)

df2 = df.unstack().copy()

df2.sort()

IDX = df2[:].index

IDX = list(reversed(IDX))


M = []

for x in IDX[1:]:
    if(x[0]==x[1]):
        continue
    M.append(x);
    if(len(M)==2):
        break;

Max1 = M[0]
Max2 = M[1]


print "Max1 : ", Max1, "->", df2[Max1] 
print "Max2 : ", Max2, "->", df2[Max2]

Output: 输出:

Max1 :  (0, 2) -> 12.293967
Max2 :  (1, 0) -> 10.87516
         ^
       column

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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