Skip to content
Advertisement

pandas countif negative using where()

Below is the code and output, what I’m trying to get is shown in the “exp” column, as you can see the “countif” column just counts 5 columns, but I want it to only count negative values.

So for example: index 0, df1[0] should equal 2

What am I doing wrong?

Python

JavaScript

Output

output

Advertisement

Answer

First DataFrame.where working different, it replace False values to 0 here by condition (here False are greater of equal 0), so cannot be used for count:

JavaScript

You need compare first 3 columns for less like 0 and sum:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement