I have a dataframe df which looks like the following:
| num1 | num2 | bool1 | bool2 | bool3 |
|---|---|---|---|---|
| 20 | 30 | True | False | True |
| 10 | 5 | False | True | True |
For each row I want to count the number of True values for a specific subset of the boolean columns, say bool2 and bool3. So the desired output would look like this:
| num1 | num2 | bool1 | bool2 | bool3 | count |
|---|---|---|---|---|---|
| 20 | 30 | True | False | True | 1 |
| 10 | 5 | False | True | True | 2 |
In SQL I used to do this with something like CARDINALITY(bool2, bool3). Trying to figure out if there is a simple way to do something like that in Pandas.
CodePudding user response:
Apply sum over columns axis:
df['count'] = df[['bool2', 'bool3']].sum(axis=1)
Output:
>>> df
num1 num2 bool1 bool2 bool3 count
0 20 30 True False True 1
1 10 5 False True True 2
