Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all the groups that meet condition at least once

I want to keep all the rows of the groups that meet a condition at least once. In other words, I want to drop the groups that do not meet the condition at least once.

I've been looking around for a few hours and could not find a solution. This was the closest I got but I could not implement the answer.

I have the following data frame:

test = pd.DataFrame({"Gr":[1,1,2,2],"Bk":[9,1,8,5]})
print(test)

   Gr  Bk
0   1   9
1   1   1
2   2   8
3   2   5

I want to group by test["Gr"] and select all the groups where test["Bk"] == 9 at least once to get to this:

# Drop Gr 2 because they didn't meet Bk == 1 in any of its rows.
   Gr  Bk
0   1   9
1   1   1

I would have thought this could be easily achieved by combining groupby() and .any() without the need of lambda functions.

I tried this:

test.groupby("Gr").filter(lambda x: (x.Bk == 9).all())
like image 705
Arturo Sbr Avatar asked Jan 01 '23 17:01

Arturo Sbr


1 Answers

Easy to understand filter

test.groupby('Gr').filter(lambda x : x['Bk'].eq(9).any())
   Gr  Bk
0   1   9
1   1   1
like image 72
BENY Avatar answered Jan 03 '23 08:01

BENY