pandas idxmax:在有联系的情况下返回所有行 [英] pandas idxmax: return all rows in case of ties

查看:116
本文介绍了 pandas idxmax:在有联系的情况下返回所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个数据框,在其中按行的权重对每一行进行加权.现在,我想选择概率最高的行,并且正在使用pandas idxmax()进行此操作,但是当有联系时,它只会返回联系的那一行中的第一行.就我而言,我想获取所有相关的行.

I am working with a dataframe where I have weight each row by its probability. Now, I want to select the row with the highest probability and I am using pandas idxmax() to do so, however when there are ties, it just returns the first row among the ones that tie. In my case, I want to get all the rows that tie.

此外,作为研究项目的一部分,我正在执行此操作,在该项目中,我正在处理数以百万计的数据帧,如下所示,因此保持快速是一个问题.

Furthermore, I am doing this as part of a research project where I am processing millions a dataframes like the one below, so keeping it fast is an issue.

示例:

我的数据如下:

data = [['chr1',100,200,0.2],
    ['ch1',300,500,0.3],
    ['chr1', 300, 500, 0.3],
    ['chr1', 600, 800, 0.3]]

从此列表中,我创建了一个熊猫数据框,如下所示:

From this list, I create a pandas dataframe as follows:

weighted = pd.DataFrame.from_records(data,columns=['chrom','start','end','probability'])

看起来像这样:

  chrom  start  end  probability
0  chr1    100  200          0.2
1   ch1    300  500          0.3
2  chr1    300  500          0.3
3  chr1    600  800          0.3

然后使用以下命令选择适合argmax(probability)的行:

Then select the row that fits argmax(probability) using:

selected =  weighted.ix[weighted['probability'].idxmax()]

当然会返回:

chrom          ch1
start          300
end            500
probability    0.3
Name: 1, dtype: object

有关系时,是否有(快速)获取所有值的方法?

Is there a (fast) way to the get all the values when there are ties?

谢谢!

推荐答案

好吧,这可能是您正在寻找的解决方案:

Well, this might be solution you are looking for:

weighted.loc[weighted['probability']==weighted['probability'].max()].T
#               1     2     3
#chrom        ch1  chr1  chr1
#start        300   300   600
#end          500   500   800
#probability  0.3   0.3   0.3

这篇关于 pandas idxmax:在有联系的情况下返回所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆