firefly
firefly

Reputation: 993

counting the amount of True/False values in a pandas row

Is there a way in pandas to calculate how many True or False conditions are there in a column.

eg: If a data frame has 5 columns and I want to select rows with at least three columns with values > 3, can this be done without the use of an iterator? So in the example below i will select rows b and c.

In [12]: df2
Out[12]: 
       A   B  C   D   E
    a  1   2  2   8   6
    b  3   6  5   8   8
    c  6   2  5   5   2

Upvotes: 7

Views: 17881

Answers (1)

YS-L
YS-L

Reputation: 14748

You can do this:

df[(df > 3).sum(axis=1) >= 3]

where df > 3 returns a Boolean mask over the entire DataFrame according to the condition, and sum(axis=1) returns the number of True in that mask, for each row. Finally the >=3 operation returns another mask that can be used to filter the original DataFrame.

Output:

   A  B  C  D  E
b  3  6  5  8  8
c  6  2  5  5  2

Upvotes: 12

Related Questions