Home > OS >  Pandas Select Rows from a dataframe with highest value within a category in separate column
Pandas Select Rows from a dataframe with highest value within a category in separate column

Time:08-05

I am looking for a way of selecting the rows of a dataframe with the highest value within a group identified in a separate column.

DataFrame:

  Group     Value
0  A         0
1  A         12
2  A         22
3  A         63
4  B         0
5  B         24
6  B         66
7  B         78

Desired DataFrame:

  Group     Value
0  A         63
1  B         78

CodePudding user response:

use groupby and take the max

df.groupby('Group').max()
        Value
Group   
A          63
B          78

CodePudding user response:

df.groupby("Group").max()

Or

df.groupby("Group")["Value"].max()
  • Related