calculations for different columns in a numpy array
<p>I have a 2D array with filled with some values (column 0) and zeros (rest of the columns). I would like to do pretty much the same as I do with MS excel but using numpy, meaning to put into the rest of the columns values from calculations based on the first column. Here it is a MWE:</p>

<pre><code>import numpy as np

a = np.zeros(20, dtype=np.int8).reshape(4,5)

b = [1, 2, 3, 4]

b = np.array(b)

a[:, 0] = b

# don't change the first column
for column in a[:, 1:]:
    a[:, column] = column[0]+1
</code></pre>

<p>The expected output:</p>

<pre><code>array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8]], dtype=int8)
</code></pre>

<p>The resulting output:</p>

<pre><code>array([[1, 0, 0, 0, 0],
       [1, 0, 0, 0, 0],
       [1, 0, 0, 0, 0],
       [1, 0, 0, 0, 0]], dtype=int8)
</code></pre>

<p>Any help would be appreciated.</p>

<p>Your usage of <code>column</code> is a little ambiguous: in <code>for column in a[:, 1:]</code>, it is treated as a column and in the body, however, it is treated as index to the column. You can try this instead:</p>

<pre><code>for column in range(1, a.shape[1]):
    a[:, column] = a[:, column-1]+1

a
#array([[1, 2, 3, 4, 5],
#       [2, 3, 4, 5, 6],
#       [3, 4, 5, 6, 7],
#       [4, 5, 6, 7, 8]], dtype=int8)
</code></pre>

python numpy