Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groupby column keep multiple rows with minimum value

I have a dataframe consisting of two columns with id's and one column with numerical values. I want to groupby the first id column and keep all the rows corresponding to the smallest values in the second column, so that I keep multiple rows if needed.

This is my pandas dataframe

id1 id2 num1
1   1   9 
1   1   4
1   2   4
1   2   3
1   3   7
2   6   9
2   6   1
2   6   5
2   9   3
2   9   7
3   2   8
3   4   2
3   4   7
3   4   9
3   4   10

What I want to have is:

id1 id2 num1
1   1   9 
1   1   4
2   6   9
2   6   1
2   6   5
3   2   8

I have tried to keep the min value, find the idxmin() or remove duplicates but this ends up with only one row per id1 and id2.

firstS.groupby('id1')['id2'].transform(min)

Many thanks in advance!

like image 939
cappuccino Avatar asked Oct 14 '25 18:10

cappuccino


1 Answers

You are close, only need compare id2 column with transform Series and filter by boolean indexing:

df = firstS[firstS['id2'] == firstS.groupby('id1')['id2'].transform(min)]
print (df)
    id1  id2  num1
0     1    1     9
1     1    1     4
5     2    6     9
6     2    6     1
7     2    6     5
10    3    2     8
like image 161
jezrael Avatar answered Oct 17 '25 07:10

jezrael