 <p>maybe try <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html" rel="nofollow noreferrer">np.apply_along_axis</a>:</p> <pre><code>&gt;&gt;&gt; def my_func(a): ... """Average first and last element of a 1-D array""" ... return (a[0] + a[-1]) * 0.5 &gt;&gt;&gt; b = np.array([[1,2,3], [4,5,6], [7,8,9]]) &gt;&gt;&gt; np.apply_along_axis(my_func, 0, b) array([ 4., 5., 6.]) &gt;&gt;&gt; np.apply_along_axis(my_func, 1, b) array([ 2., 5., 8.]) </code></pre>