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.

Macter
  • 132
  • 1
  • 12
  • See also: https://stackoverflow.com/questions/33823091/python-pandas-counting-the-frequency-of-a-specific-value-in-each-row-of-datafra https://stackoverflow.com/questions/49557895/replace-specific-values-in-pandas-dataframe – i vk Dec 09 '21 at 19:17

1 Answers1

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
Corralien
  • 109,409
  • 8
  • 28
  • 52