I have a pandas dataframe that looks like this:
| name | category | status |
|---|---|---|
| John | student | yes |
| Jane | employee | no |
| Elijah | student | no |
| Anne | student | yes |
| Elle | employee | no |
I want to count the number of each categories that have status 'yes'
I have tried 2 codes below:
(DataFrame['status'].eq('yes').groupby(DataFrame['category']).nunique())(DataFrame['status'].eq('yes').groupby(DataFrame['category']).any().sum())
both codes give the same output:
category
student 2
employee 1
but, this is the output that I expect:
category
student 2
employee 0
can you help me fix this?
If need count
Trues values need aggregatesum, becauseTrues are processing like1andFalselike0:If aggregate
nuniqueget count of unique values in firstTrue, Falsereturn2and in secondNoreturn1(noYesfor second group).For testing check unique values per groups:
If use
anyit test if at least oneTrueper groups, so ouput is different: