繁体   English   中英

如何 select pandas 行在一个列中具有最大值,来自一组共享两个公共列的行?

[英]How to select pandas row with maximum value in one column, from a group of rows that share two common columns?

下面的 Pandas DataFrame df有 5 列,彩色,而索引号在最左边的黑色。

在此处输入图像描述

请注意最后两列(我们称它们为col4col5 )具有 static 编号,表示数据的段、组或块。 其他组(在这两列中更改其 static 编号)已从屏幕截图中隐藏。

如何挑出第三列(称为col3 )中具有最大值的行或行的索引,用黑色圈出: 1.90977 ,条件是最后两行是 static? 换句话说,挑出组中最好的行

寻找这样的东西,这是行不通的:

df.loc[(df['col3'] == 0.999141) & (df['col4'] == 0.000861559)]

If not last 2 columns has same values use numpy.isclose for select columns by some precision, also for performance is better select by DataFrame.loc by mask and column name:

df.loc[np.isclose(df['col4'], 0.999141) & np.isclose(df['col5'], 0.000861559), 'col3'].max()

对于最大值使用Series.idxmax的索引:

df.loc[np.isclose(df['col4'], 0.999141) & np.isclose(df['col5'], 0.000861559), 'col3'].idxmax()

对于 select 通过最大col4和最小col5使用:

df.loc[df['col4'].eq(df['col4'].max()) & df['col5'].eq(df['col5'].min()), 'col3'].max()

df.loc[df['col4'].eq(df['col4'].max()) & df['col5'].eq(df['col5'].min()), 'col3'].idxmax()

暂无
暂无

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

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