The Unfun Cat
The Unfun Cat

Reputation: 31908

Get number of specific element in each row

I have a dataframe, like the following:

True True
False True

I would like to count the number of True values for each row, something like

df.count(value=True, axis=1)

or

df.apply(count(False), axis=1)

It should return 2 1.

How do I do that?

Upvotes: 1

Views: 50

Answers (1)

John Zwinck
John Zwinck

Reputation: 249143

You can do it like this:

df.sum(axis=1)

This relies on the fact that True is counted as 1 and False as 0, and is optimally efficient.

Upvotes: 2

Related Questions