1

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?

datafreak
  • 39
  • 5

1 Answers1

2

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
Psidom
  • 209,562
  • 33
  • 339
  • 356