I want to delete all instances of a value in a numpy array which is shaped in rows.
a = array([5,1,2,4,9,2]).reshape(-1, 1)
print("before delete", a)
a = np.delete(a, np.where(a == 2))
print("after delete", a)
The output is
before delete [[5]
[1]
[2]
[4]
[9]
[2]]
after delete [1 4 9]
I don't know why 5 is deleted. I expect to see
[[5]
[1]
[4]
[9]]
How can I fix that?
CodePudding user response:
np.where in this case returns 2 arrays. The first one is the indexes of the values that are equal to 2, but the second is not needed. You need to grab the first one:
a = np.delete(a, np.where(a == 2)[0])
Output:
>>> a
array([5, 1, 4, 9])
