title: Numpy: conditional sum
body: <p>I have the following numpy array:</p>
  
  <pre><code>import numpy as np
  arr = np.array([[1,2,3,4,2000],
                  [5,6,7,8,2000],
                  [9,0,1,2,2001],
                  [3,4,5,6,2001],
                  [7,8,9,0,2002],
                  [1,2,3,4,2002],
                  [5,6,7,8,2003],
                  [9,0,1,2,2003]
                ])
  </code></pre>
  
  <p>I understand <code>np.sum(arr, axis=0)</code> to provide the result:</p>
  
  <pre><code>array([   40,    28,    36,    34, 16012])
  </code></pre>
  
  <p>what I would like to do (<strong>without a for loop</strong>) is sum the columns based on the value of the last column so that the result provided is:</p>
  
  <pre><code>array([[   6,    8,   10,   12, 4000],
         [  12,    4,    6,    8, 4002],
         [   8,   10,   12,    4, 4004],
         [  14,    6,    8,   10, 4006]])
  </code></pre>
  
  <p>I realize that it may be a stretch to do without a loop, but hoping for the best...</p>
  
  <p>If a for loop must be used, then how would that work?</p>
  
  <p>I tried <code>np.sum(arr[:, 4]==2000, axis=0)</code> (where I would substitute <code>2000</code> with the variable from the for loop), however it gave a result of <strong><code>2</code></strong></p>