I need to select only the rows that have the minimum 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?
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With