Let's say I have a dataframe
Category Data1 column1
A 'SOMEDATA' 10
A 'SOMEDATA' 2
A 'SOMEDATA' -10
B 'SOMEDATA' 10
B 'SOMEDATA' 1
B 'SOMEDATA' -10
and so on
I'd like to select a one row in each group by column value. For example, ABS(column1)
So resulting data is
Category Data1 column1
A 'SOMEDATA' 2
B 'SOMEDATA' 1
How can I do this in python?
I couldn't figure out how to return entire row. For example,
df.groupby('Category')['column1'].min();
this would only return 'Category' min(column1) only.
Here is a solution that is more computationally efficient.
TL;DR version
df.loc[df.groupby('Category')['column1'].idxmin()]
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