python pandas conditional count across columns python pandas conditional count across columns pandas pandas

python pandas conditional count across columns


I'm just doing this with a flat dataframe but it's the same for panel. You can do one of two ways. The first way is what you did, just change the count() to sum():

( df > 0 ).sum(axis=1)

The underlying structure is boolean and True and False both get counted, whereas if you sum them it is interpreted more like you were expecting (0/1).

But a more standard way to do it would be like this:

df[ df > 0 ].count(axis=1)

While the former method was based on a dataframe of booleans, the latter looks like this:

df[ df > 0 ]    a   b   c   d   e   f   g   h   i   j0   1 NaN NaN NaN NaN NaN NaN NaN   1 NaN1 NaN   1 NaN NaN NaN   1 NaN NaN NaN NaN2   1 NaN NaN NaN NaN NaN NaN NaN NaN NaN3 NaN NaN NaN NaN NaN NaN NaN   1 NaN NaN4 NaN NaN NaN   1 NaN NaN NaN NaN NaN NaN

In this case it doesn't really matter which method you use, but in general the latter is going to be better, because you can do more with it. For example, with the former method (which has binary outcomes by design), all you can really do is count, but in the latter method you can count, sum, multiply, etc.

The potential usefulness of this may be more obvious for the case of df != 0, where there are more than two possible values:

df[ df != 0 ]    a   b   c   d   e   f   g   h   i   j0   1 NaN NaN  -1 NaN NaN  -1 NaN   1 NaN1 NaN   1 NaN NaN NaN   1 NaN NaN NaN  -12   1 NaN NaN NaN NaN  -1 NaN NaN NaN NaN3 NaN  -1 NaN NaN NaN NaN NaN   1 NaN NaN4 NaN NaN NaN   1 NaN NaN  -1 NaN NaN  -1