If I have an array: A = np.array([[1,2,0],[5,6,0]])
. How can I replace the third column with the sum of the first two or some other arithmetic combination of the other columns?
In the example, calculating the third column as the sum of the sum of the first 2 would give: np.array([[1,2,3],[5,6,11]])
.
I've tried A[:2] = A[:,0] + A[:,1]
and A[:2] = A[:,0].T + A[:,1].T
. I've searched for adding columns, but found ways to insert columns.
import numpy as np
A = np.array([[1,2,3],[5,6,7]])
A[:2] = A[:,0] + A[:,1]
In R this is very easy, but I don't see a simple way to do it in Python.