1

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.

1

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.