{"id":2855,"date":"2023-10-17T09:38:58","date_gmt":"2023-10-17T04:08:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=2855"},"modified":"2023-11-10T11:45:02","modified_gmt":"2023-11-10T06:15:02","slug":"find-the-index-of-value-in-numpy-array-using-numpy-where","status":"publish","type":"post","link":"https:\/\/python-programs.com\/find-the-index-of-value-in-numpy-array-using-numpy-where\/","title":{"rendered":"Find the index of value in Numpy Array using numpy.where()"},"content":{"rendered":"
NumPy or Numerical Python which is a general purpose array processing package and it is used for working with arrays. In python there Where<\/p>\n In this article we will discuss about how to get the index of an element in a Numpy array (both 1D & 2D) using this function. So, let’s explore the concept well.<\/p>\n Suppose Now, let’s find the index number of 30 in this Numpy array.<\/p>\n As result is a tuple of arrays which contains the indices where value 30 exists in array array_one i.e. so it returned<\/p>\n And as a flat 1D array it returned<\/p>\n And if the element is not present in the Numpy array then returned array of indices will be empty. So let’s check that with an example<\/p>\n Suppose Now, let’s find the index number of 30 in this Numpy array.<\/p>\n Where,<\/p>\n First array [1, 3, 5] represents row indices<\/em><\/strong> where the element 30 is present<\/p>\n And the second array [1<\/span>, 0<\/span>, 1] represents column indices<\/strong><\/em> where the element 30 is present.<\/p>\n 3.<\/strong> Get indices of elements based on multiple conditions in Numpy :<\/strong><\/p>\n Example : 1<\/strong><\/p>\n Get the indices of elements with value less than 20 and greater than 10.<\/p>\n Example : 2<\/strong><\/p>\n Get the first index of an element in numpy array.<\/p>\n Example : 3<\/strong><\/p>\n Get the indices of the elements which is less than equal to 2<\/p>\n How to find the index of value in Numpy array using numpy.where() in Python ? NumPy or Numerical Python which is a general purpose array processing package and it is used for working with arrays. In python there numpy.where( ) function which is used to select elements from a numpy array, based on a condition …<\/p>\nnumpy.where( )<\/code> function which is used to select elements from a numpy array, based on a condition and if the condition is satisfied we perform some operations.<\/p>\n
Syntax : numpy<\/span>.where<\/span>(condition[ , x, y ]<\/span>)<\/pre>\n
\n
1. Find index of a value in 1D Numpy array :<\/h3>\n
array_one<\/code> is an 1D Numpy array, So it will look like<\/p>\n
array_one = np.<\/span>array<\/span>([<\/span>10<\/span>, 15<\/span>, 2<\/span>0<\/span>, 25<\/span>, 30<\/span>, 35<\/span>, 4<\/span>0<\/span>, 45<\/span>, 50<\/span>, 30<\/span>, 60<\/span>, 6<\/span>5<\/span>, 70<\/span>, 75, 80, 85, 30, 95<\/span>])<\/span><\/pre>\n
Program :\r\n\r\nimport numpy as np\r\n# numpy array created with a list of numbers\r\narray_one = np.array([10, 15, 20, 25, 30, 35, 40, 45, 50, 30, 60, 65, 70, 75, 80, 85, 30, 95])\r\n# Get the index of elements with value 30\r\noutput = np.where(array_one == 30)\r\n#printing the indices where the element 30 is present\r\nprint('Tuple of arrays returned : ', output)\r\nprint(\"Elements with value 30 exists at following indices\", output[0], sep='\\n')<\/pre>\n
Output :\r\nTuple of arrays returned : (array([4, 9, 16]), )\r\nElements with value 30 exists at following indices \r\n[4 9 16]\r\n<\/span><\/pre>\n<\/div>\n
(array([4, 9, 16]), )<\/span><\/pre>\n
[4 9 16]<\/span><\/pre>\n
Program :\r\n\r\nimport numpy as np\r\n# numpy array created with a list of numbers\r\narray_one = np.array([10, 15, 20, 25, 30, 35, 40, 45, 50, 30, 60, 65, 70, 75, 80, 85, 30, 95])\r\n# Get the index of elements with value 100\r\noutput = np.where(array_one == 100)\r\nprint('Tuple of arrays returned : ', output)\r\n#printing the indices where the element 100 is present\r\n#As 100 is not present in Numpy array i.e array_one so returned array with indices is empty \r\nprint(\"Elements with value 30 exists at following indices\", output[0], sep='\\n')\r\n<\/pre>\n
Output :\r\n\r\nTuple of arrays returned : (array([ ]), )\r\nElements with value 30 exists at following indices\r\n[ ]\r\n<\/span><\/pre>\n
2. Find index of a value in 2D Numpy array :<\/h3>\n
array_two<\/code> is an 2D Numpy array, So it will look like<\/p>\n
array_two = np.<\/span>array<\/span>([ [<\/span>10<\/span>, 15<\/span>, 2<\/span>0],<\/span> [25<\/span>, 30<\/span>, 35]<\/span>, [4<\/span>0<\/span>, 45<\/span>, 50]<\/span>, [30<\/span>, 60<\/span>, 6<\/span>5]<\/span>, [70<\/span>, 75, 80], [85, 30, 95<\/span>] ])<\/span><\/pre>\n
Program :\r\n\r\nimport numpy as np\r\n#\u00a0numpy\u00a0array\u00a0created\u00a0with\u00a0a\u00a0list\u00a0of\u00a0numbers\r\narray_two = np.array([ [10, 15, 20], [25, 30, 35], [40, 45, 50], [30, 60, 65], [70, 75, 80], [85, 30, 95] ])\r\n# Get the index of elements with value 30\r\noutput = np.where(array_two == 30)\r\n#printing the indices where the element 30 is present\r\nprint('Tuple of arrays returned: ', output)\r\nprint(\"Elements with value 30 first exists at index:\", output[0][0])<\/pre>\n
Output :\r\nTuple<\/span> of<\/span> arrays returned: (array<\/span>([1, 3, 5]), array<\/span>([1<\/span>, 0<\/span>, 1])) \r\nElements with<\/span> value 30 first exists at index<\/span>: 1<\/pre>\n
Program :\r\n\r\nimport numpy as np\r\n# numpy array created with a list of numbers\r\nexample_array = np.array([11, 6, 13, 8, 15, 16, 7, 15, 1, 2, 14, 19, 4, 20])\r\n# Get the index of elements of value less than 20 and greater than 10\r\noutput = np.where((example_array > 10) & (example_array < 20))\r\nprint(\"Elements which are less than 20 and greater than 10 exists at indices\",output, sep='\\n')<\/pre>\n
Output :\r\nElements which are<\/span> less than 20<\/span>\u00a0and<\/span> greater than <\/span>10<\/span> exists at <\/span>indices \r\n<\/span>(<\/span>array<\/span>([<\/span> 0, 2<\/span>, <\/span>4<\/span>, 5<\/span>, 7<\/span>, <\/span>10, 11<\/span>]<\/span>)<\/span>,<\/span>)<\/span><\/pre>\n
Program :\r\n\r\nimport numpy as np\r\n#\u00a0numpy\u00a0array\u00a0created\u00a0with\u00a0a\u00a0list\u00a0of\u00a0numbers\r\nexample_array = np.array([15, 6, 13, 8, 15, 16, 7, 15, 1, 2, 14, 19, 4, 20])\r\n# Get the index of elements with value less than 20 and greater than 10\r\noutput = np.where(example_array == 15)\r\nif len(output) > 0 and len(output[0]) > 0:\r\n print('First Index of element with value 15 is ', output[0][0])<\/pre>\n
Output :\r\nFirst Index of element with value 15 is 0<\/pre>\n
Program :\r\n\r\nimport numpy as np \r\narr = np.arange(10)\r\n#prints the array\r\nprint(arr)\r\n#prints indices of the elements which is less than equal to 2\r\nprint(arr[arr <= 2])<\/pre>\n
Output :\r\n[0 1 2 3 4 5 6 7 8 9] \r\n[0 1 2]<\/pre>\n","protected":false},"excerpt":{"rendered":"