Hi,
I would like to select rows from a 2D numpy array according to a condition. I would like to get a 2D results array directly without the need of the costly np.reshape statement.
Does anybody know how to assemble array b more efficiently?
Thanks,
Alexander
>>> a = np.array([[0, 0.01, 1], [1, 0.02, 1], [2, 0.03, 2], [3, 0.04, 2], [4, 0.05, 3], [5, 0.06, 3]])
>>> a
array([[ 0. , 0.01, 1. ],
[ 1. , 0.02, 1. ],
[ 2. , 0.03, 2. ],
[ 3. , 0.04, 2. ],
[ 4. , 0.05, 3. ],
[ 5. , 0.06, 3. ]])
>>> b = np.empty(0)
>>> for i in range(a.shape[0]):
... if a[i,2] == 1.0:
... b = np.append(b, a[i], axis=0)
>>> b = b.reshape(b.shape[0]/a.shape[1], a.shape[1])
>>> b
array([[ 0. , 0.01, 1. ],
[ 1. , 0.02, 1. ]])