0

I have an array like this:

name  foo     bar
   A    0  [3, 1]
   A    2  [2, 3] *
   B    3  [1, 0]
   B    9  [0, 1] *
   B    1  [1, 0]

I would like to keep, for each unique name, the row that hax the maximum bar[1] value (marked with a * above), so that the result should be

name  foo     bar
   A    2  [2, 3]
   B    9  [0, 1]

There is this question and the related answer that is very closely related to my problem, however in that question, the max is done directly on the columns value. If GroupBy.max accepted a key function like the built-in max then this would be a no-brainer -- however this is not the case.

user209974
  • 1,737
  • 2
  • 15
  • 31

1 Answers1

4

You can use indexing by str for select second value of lists and pass to DataFrameGroupBy.idxmax for indices and select by loc:

df.loc[df.bar.str[1].groupby(df['name']).idxmax()]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252