How to find the count of same values in a row in a dataframe?

Question:

The dataframe is as follows:

a     |   b    |   c    |   d
-------------------------------
TRUE    FALSE    TRUE     TRUE
FALSE   FALSE    FALSE    TRUE
TRUE    TRUE     TRUE     TRUE
TRUE    FALSE    TRUE     FALSE

I need to find the count of the TRUE’s in each column.
The last row should contain the count as follows:

a     |   b    |   c    |   d  |  count
---------------------------------------
TRUE    FALSE    TRUE     TRUE     3
FALSE   FALSE    FALSE    TRUE     1
TRUE    TRUE     TRUE     TRUE     4
TRUE    FALSE    TRUE     FALSE    2

The logic I tried is:

df.groupby(df.columns.tolist(),as_index=False).size()

But it doesn’t work as expected.
Could anyone please help me out here?
Thank you.

Asked By: Cosmo

||

Answers:

Because Trues are processing like 1 you can use sum:

df['count'] = df.sum(axis=1)

If TRUEs are strings:

df['count'] = df.eq('TRUE').sum(axis=1)
Answered By: jezrael
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.