简体   繁体   English

Pandas Groupby-如果多行超过另一行的值,则选择一列中值最高的行

[英]Pandas Groupby - select row with highest value in one column if multiple rows exceed value in another

This operation groups my DataFrame by two columns, then returns the row with the highest value in ColumnC : 此操作将DataFrame按两列进行分组,然后返回ColumnC具有最高值的ColumnC

df2 = df.loc[df.groupby(['columnA', 'columnB'], sort=False)['columnC'].idxmax()]

Instead, for all rows where ColumnC > 100 within each group, I would like to take the row with the highest value in ColumnD . 相反,对于每个组中ColumnC > 100所有行,我想采用ColumnD具有最高值的ColumnD

How can I do this? 我怎样才能做到这一点?

Edit: 编辑:

Comment below by @Code Different is basically what I'm looking for, but I don't want to exclude groups where none of the rows have ColumnC > 100 , in these cases I want the row with the highest value in ColumnC , as in the example above. @Code Different在下面的注释基本上是我要查找的内容,但是我不想排除没有任何行的ColumnC > 100 ,在这种情况下,我希望在ColumnC具有最高值的ColumnC ,如上面的例子。

Usually we split the data by two part , then filter them after the condition 通常,我们将数据分为两部分,然后在条件满足后进行过滤

df=sort_values('columnD')

df1 = df[df['columnC'] > 100]].drop_duplicates(['columnA', 'columnB'],keep='last')
df2 = df.drop_duplicates(['columnA', 'columnB'],keep='last')

Yourdf=pd.concat([df1,df2]).drop_duplicates(['columnA', 'columnB'])

暂无
暂无

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

相关问题 熊猫groupby并检查另一行的值是否在另一行的值内 - Pandas groupby and check if value of one row within another row value 从 groupby 中选择具有最高值的行 - Select rows with highest value from groupby 熊猫:复制一行的值并将其粘贴到另一列中的多行上方 - Pandas: Copy a value of a row and paste it to multiple rows above in another column 如何在熊猫中使用groupby保持具有最高值的另一列的值 - how to keep the value of a column that has the highest value on another column with groupby in pandas Pandas GroupBy 和 select 行在特定列中具有最小值 - Pandas GroupBy and select rows with the minimum value in a specific column 如何在 pandas groupby() 组的列中使用最大值 select 行? - How to select row with max value in column from pandas groupby() groups? pandas groupby,然后按列的值选择一行(例如,最小值、最大值) - pandas groupby and then select a row by value of column (min,max, for example) 当列值在另一行列值的范围内时,Pandas会选择行 - Pandas select rows when column value within range from another row column value 使用组过滤器,当列值在另一个行列值的范围内时,熊猫选择行 - Pandas select rows when column value within range from another row column value with group filter 在 Pandas 中选择行,其中一列中的值是另一列中值的子字符串 - Select rows in pandas where value in one column is a substring of value in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM