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())
Easy to understand filter
test.groupby('Gr').filter(lambda x : x['Bk'].eq(9).any())
Gr Bk
0 1 9
1 1 1
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