2

I just filtered some data and now I have a .csv file, but I just noticed that I need to select only the rows that have the minimun price:

example:

ORIGIN   | DESTINA. | PRICE
____________________________
BOG      | MAD      |  1500
BOG      | MAD      |  750
BOG      | MAD      |  1250
BOG      | MAD      |  1350
BOG      | MIA      |   450

So in this example, what I would like to get is only the third AND sixth row:

ORIGIN   | DESTINA. | PRICE
____________________________
BOG      | MAD      | 750
BOG      | MIA      | 450

Using python, how can I get this final table?

jpp
  • 159,742
  • 34
  • 281
  • 339
rubio119
  • 191
  • 1
  • 2
  • 6

1 Answers1

9

Using GroupBy + transform with min:

df = df[df['PRICE'] == df.groupby('ORIGIN')['PRICE'].transform('min')]

This will keep duplicate groupwise minima. If you don't want to keep duplicates, you can sort, then drop duplicates:

df = df.sort_values('PRICE').drop_duplicates('ORIGIN')
jpp
  • 159,742
  • 34
  • 281
  • 339