Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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)
edited by

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 Data Science with Python Course and be a master in it 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...