title: Summing blocks of N rows in NumPy array
body: <p>This probably is a duplicate question but I can't find the exact solution I need.</p>
  
  <p>I am trying to sum every N rows (say 4 in this example) together. So in an 8 by 9 matrix, I would end up with a 2 by 9 array i.e. summing rows 0-3 together and then summing rows 4-7 together. Right now, this is the solution that I have but is there a way not to use list comprehension? Is there a more "numpy" way to do this? Because I end up with a list of 2 1-by-9 arrays rather than a single 2-by-9 array.</p>
  
  <p>The input array is not fixed to 8-by-9, it can be 12-by-9 or 28-by-9 but the total number of rows of the input array will always be an integer multiple of N (which is 8 in this example and N=4)</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; a = np.arange(72).reshape(8,9)
  &gt;&gt;&gt; a
  array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
     [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
     [18, 19, 20, 21, 22, 23, 24, 25, 26],
     [27, 28, 29, 30, 31, 32, 33, 34, 35],
     [36, 37, 38, 39, 40, 41, 42, 43, 44],
     [45, 46, 47, 48, 49, 50, 51, 52, 53],
     [54, 55, 56, 57, 58, 59, 60, 61, 62],
     [63, 64, 65, 66, 67, 68, 69, 70, 71]])
  
  &gt;&gt;&gt; b = [a[i:(i+1)*4,:] for i in range(0,len(a),4)]
  
  &gt;&gt;&gt; b
     [array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
     [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
     [18, 19, 20, 21, 22, 23, 24, 25, 26],
     [27, 28, 29, 30, 31, 32, 33, 34, 35]]), 
     array([[36, 37, 38, 39, 40, 41, 42, 43, 44],
     [45, 46, 47, 48, 49, 50, 51, 52, 53],
     [54, 55, 56, 57, 58, 59, 60, 61, 62],
     [63, 64, 65, 66, 67, 68, 69, 70, 71]])]
  
  &gt;&gt;&gt; b = [np.sum(a[i:(i+1)*4,:],axis=0) for i in range(0,len(a),4)]
  
  &gt;&gt;&gt; b
  
  &gt;&gt;&gt;[array([54, 58, 62, 66, 70, 74, 78, 82, 86]), array([198, 202, 206, 210, 
     214, 218, 222, 226, 230])]
  </code></pre>