0

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
  • 1
  • 4
  • 26
  • 35
hiva
  • 13
  • 3

2 Answers2

3

What you need is

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

Here's a one liner to do this:

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

YOLO
  • 18,072
  • 3
  • 18
  • 39