Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Data Science by (18.4k points)

I know how to replace all the elements of a NumPy array that are greater than some of the values, like array[array > 0] = 0, but I don't know how to replace all the elements that are equal to some values without using the for loop, like below can I achieve what I want but is there any way not to use the for loop?

Sorry for being unclear, some_values here is a list, like [7, 8, 9]

for v in some_values:

    array[array == v] = 0

1 Answer

0 votes
by (36.8k points)

Try to use the np.isin:

array[np.isin(array, some_values)] = 0

Want to gain skills in Data Science with Python? Sign up today for this Python for Data Science Course and be a master in it 

Browse Categories

...