简体   繁体   English

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

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

The following Pandas DataFrame df has 5 columns, colored, while the index numbers are on the very left in black.下面的 Pandas DataFrame df有 5 列,彩色,而索引号在最左边的黑色。

在此处输入图像描述

Notice the last two columns (let's call them col4 and col5 ) have static numbers, denoting a segment, group or chunk of the data.请注意最后两列(我们称它们为col4col5 )具有 static 编号,表示数据的段、组或块。 Other groups (that change their static numbers in these two columns) have been hidden from screenshot.其他组(在这两列中更改其 static 编号)已从屏幕截图中隐藏。

How to single out the row, or index of the row, that has the largest value in the third column (called col3 ), circled in black: 1.90977 , conditional on the fact that the last 2 rows are static?如何挑出第三列(称为col3 )中具有最大值的行或行的索引,用黑色圈出: 1.90977 ,条件是最后两行是 static? In other words, single out the best row in the group换句话说,挑出组中最好的行

looking for something like this, which doesn't work:寻找这样的东西,这是行不通的:

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

For index of maximum value use Series.idxmax :对于最大值使用Series.idxmax的索引:

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

For select by maximum col4 and minimum col5 use:对于 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.

相关问题 Python / Pandas - 如何按两列分组,并计算两行之间第三列的值 - Python/Pandas - How to group by two columns and count rows with value from third column between two numbers 使用组过滤器,当列值在另一个行列值的范围内时,熊猫选择行 - Pandas select rows when column value within range from another row column value with group filter Pandas 如何在 select 列中一行具有最大值? - Pandas how to select columns where a row has maximum value? Pandas 在一个 dataframe 中删除与另一个 dataframe 的列中的行共享一个共同值的行 - Pandas drop rows in one dataframe that share a common value with a rows in a column of another dataframe 将 Pandas dataframe 分组为两列, output 将最大列值指示到新列 - Group Pandas dataframe by two columns and output the maximum column value indication to new column 如何根据行中的特定值和熊猫中的另一列对行进行分组? - How to group rows based on specific value in a row and another column in pandas? Pandas groupby 根据列值和组大小份额选择前 N 行 - Pandas groupby select top N rows based on column value AND group size share 我有一个带有列表的 pandas 列。 对包含来自同一列的至少一个公共元素的行进行分组 - I have a pandas column with lists. Group rows that contains atleast one common element from same column 在 pandas 中,如果列(或列的子集)中的任何值是常见的,如何将行组合在一起? - In pandas, how to group row together if any value in the columns (or subset of columns) is common? Select Pandas dataframe 行,其中两列或多列一起具有最大值 - Select Pandas dataframe row where two or more columns have their maximum value together
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM