Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas groupby and then select a row by value of column (min,max, for example)

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.

like image 665
Nite Avatar asked Mar 15 '19 15:03

Nite


1 Answers

Here is a solution that is more computationally efficient.

TL;DR version

df.loc[df.groupby('Category')['column1'].idxmin()]
like image 73
Amirkhm Avatar answered Nov 03 '22 03:11

Amirkhm