I have the following numpy array:
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]
])
I understand np.sum(arr, axis=0)
to provide the result:
array([ 40, 28, 36, 34, 16012])
what I would like to do (without a for loop) is sum the columns based on the value of the last column so that the result provided is:
array([[ 6, 8, 10, 12, 4000],
[ 12, 4, 6, 8, 4002],
[ 8, 10, 12, 4, 4004],
[ 14, 6, 8, 10, 4006]])
I realize that it may be a stretch to do without a loop, but hoping for the best...
If a for loop must be used, then how would that work?
I tried np.sum(arr[:, 4]==2000, axis=0)
(where I would substitute 2000
with the variable from the for loop), however it gave a result of 2