0

I wanna grab the maximum value of column score for common values in first column

example

f    score
A    4
B    5
A    6
A    0
C    1
C    4
Y    2

output

f    score
A    6
B    5
C    4
Y    2

explanation: the max score when f[i]==A is 6 and when f[i]==C is 4

Andrej Kesely
  • 118,151
  • 13
  • 38
  • 75

1 Answers1

0

You can use pandas.DataFrame.groupby and pandas.Series.max:

df.groupby('f', as_index=False, sort=False).max()

output:

   f  score
0  A      6
1  B      5
2  C      4
3  Y      2

*NB. depending on the use case, the sort=False option in groupby can be set to keep the original order of the groups (which doesn't make a difference with this dataset).

mozway
  • 81,317
  • 8
  • 19
  • 49