Home > Blockchain >  Python pandas: Retrieve highest value of each rows with TIES [duplicate]
Python pandas: Retrieve highest value of each rows with TIES [duplicate]

Time:09-25

I have a dataframe which produces this table.

    Row Number  Rank
0   702          20
1   702          20
2   702          6
3   100          5
4   100          5
5   100          1

I want to be able to filter this dataframe so that it only keeps the rows with the highest value in "Rank" (including ties) by each Row Number. Like this example:

    Row Number  Rank
0   702          20
1   702          20
2   100          5
3   100          5

How can I perform this?

CodePudding user response:

Calculate the max values per Row Number using groupby.transform('max') and then filter:

df[df.Rank == df.Rank.groupby(df['Row Number']).transform('max')]

   Row Number  Rank
0         702    20
1         702    20
3         100     5
4         100     5
  • Related