1

I have a data frame that has some rows with the same ID which includes different starcounter values. the raw DataFrame

I need to keep the rows with a minimum value and delete the extra rows to reach this table: Selected rows.

Thank you in advance.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
hiva
  • 23
  • 4

2 Answers2

4

What you need is

df2 = df.sort_values('starcounter').drop_duplicates(['ID'], keep='first')
GihanDB
  • 591
  • 2
  • 6
  • 23
1

Here's a one liner to do this:

df.loc[df.groupby('ID')['starcounter'].idxmin()]

YOLO
  • 20,181
  • 5
  • 20
  • 40