title: pandas countif negative using where()
body: <p>Below is the code and output, what I'm trying to get is shown in the &quot;exp&quot; column, as you can see the &quot;countif&quot; column just counts 5 columns, but I want it to only count negative values.</p>
  <p>So for example: index 0, <code>df1[0]</code> should equal <code>2</code></p>
  <p>What am I doing wrong?</p>
  <p>Python</p>
  <pre><code>import pandas as pd
  import numpy as np 
  
  a = ['A','B','C','B','C','A','A','B','C','C','A','C','B','A']
  b = [2,4,1,1,2,5,-1,2,2,3,4,3,3,3]
  c = [-2,4,1,-1,2,5,1,2,2,3,4,3,3,3]
  d = [-2,-4,1,-1,2,5,1,2,2,3,4,3,3,3]
  exp = [2,1,0,2,0,0,1,0,0,0,0,0,0,0]
  
  df1 = pd.DataFrame({'b':b,'c':c,'d':d,'exp':exp}, columns=['b','c','d','exp'])
  df1['sumif'] = df1.where(df1&lt;0,0).sum(1)
  df1['countif'] = df1.where(df1&lt;0,0).count(1)
  df1
  # df1.sort_values(['a','countif'], ascending=[True, True])
  </code></pre>
  <p>Output</p>
  <p><a href="https://i.stack.imgur.com/s4CLa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s4CLa.png" alt="output" /></a></p>