← Back to NumPy Course | Chapter 10: Boolean & Comparison | Lesson 2 of 6

np.where() का इस्तेमाल

np.where() एक condition के आधार पर हर element के लिए दो values के बीच चुनता है, पूरे arrays के लिए एक if/else की तरह।

In this page:

  1. np.where()
Syntax
python
result = np.where(condition, x, y)

np.where()

np.where(condition, x, y) जहाँ condition True हो वहाँ x return करता है और बाकी जगह y। सिर्फ एक condition के साथ यह True elements के indices return करता है। यह if/else वाले एक loop का vectorized replacement है।

Note: सिर्फ एक argument के साथ, np.where index arrays का एक tuple return करता है, प्रति dimension एक।

उदाहरण: np.where()

python
import numpy as np

scores = np.array([45, 82, 67, 90, 30])
print(np.where(scores >= 50, "pass", "fail"))
print(np.where(scores > 60))

# Output:
# ['fail' 'pass' 'pass' 'pass' 'fail']
# (array([1, 2, 3]),)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि one-argument form एक tuple return करता है
  2. incompatible shapes वाले arrays pass करना
  3. इसकी बजाय एक loop के अंदर एक Python if/else इस्तेमाल करना
चैप्टर सारांश
  • where(cond, x, y) प्रति element चुनता है
  • एक argument indices return करता है
  • x और y broadcast करता है
  • loops की जगह if/else से लेता है
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.