Reputation: 4490
I am trying to find, for every id: the number of values equal to 0.0 and greater than 0.0
Input DF:
ID . value
1 . 0.0
1 . 10.0
1 . 30.0
1 . 0.0
1 . 25.0
2 . 0.0
2 . 4.0
2 . 0.0
2 . 13.0
Output DF:
id . count (value = 0.0) . count(value > 0.0)
1 2 3
2 . 2 2
Code:
df.groupby("ID")["value"].nunique()
which just returns the number of unique values for each ID, I am not sure how to combine this if-else of (0.0 and value>0.0) in that command. Any suggestions please
Upvotes: 2
Views: 184
Reputation: 2894
Try this:
df['equal'] = df.groupby('ID').apply(lambda x: sum(x==0))
df['greater'] = df.groupby('ID').apply(lambda x: sum(x>0))
Upvotes: 0
Reputation: 323306
You can do with
pd.crosstab(df.ID,df.value.gt(0))
Out[392]:
value False True
ID
1 2 3
2 2 2
Upvotes: 3