Reputation: 191
I have the following line in my code where I group df based on a column Package
, and calculate the size of each group based on a criteria on another column Id
.
df.groupby("Package")["Id"].apply(lambda x: x.isin(someList).sum())
Package
P1 1
P2 12
P3 52
P4 123
P5 5421
P6 53
P7 64
Name: Id, dtype: int64
My question is that I also need to calculate the size of each group where x
is not in someList
. How can I do that? Should I just tilde somewhere?
Upvotes: 1
Views: 1476
Reputation: 862841
In your solution add ~
with parentheses to lambda function:
df.groupby("Package")["Id"].apply(lambda x: (~x.isin(someList)).sum())
Or use syntactic sugar - create Series
of not membership and aggregate by Series df["Package"]
:
(~df["Id"].isin(someList)).groupby(df["Package"]).sum()
Similar cleaner idea with column Id
filled by mask, so possible aggregate with column name:
df.assign(Id = ~df["Id"].isin(someList)).groupby("Package")["Id"].sum()
Upvotes: 1