{"id":3391,"date":"2021-04-27T19:37:08","date_gmt":"2021-04-27T14:07:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=3391"},"modified":"2021-11-22T18:53:44","modified_gmt":"2021-11-22T13:23:44","slug":"numpy-where-explained-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/numpy-where-explained-with-examples\/","title":{"rendered":"numpy.where() \u2013 Explained with examples"},"content":{"rendered":"
In this article, we will see various examples of numpy.where()\u00a0<\/em>function and how it works in python. For instance, like,<\/p>\n In Python’s NumPy module, we can select elements with two different sequences based on conditions on the different array.<\/p>\n Explanation:<\/p>\n Let’s dig in to see some examples.<\/p>\n Let’s say we have two lists of the same size and a NumPy array.<\/p>\n Now, we want to convert this numpy array to the array of the same size, where the values will be included from the list high_values<\/em> and low_values<\/em>. For instance, if the value in an array is less than 12, then replace it with the ‘low<\/em>‘ and if the value in array arr is greater than 12 then replace it with the value ‘high<\/em>‘.<\/p>\n So ultimately, the array will look like this:<\/p>\n We can also do this with for loops, but this numpy module is designed to carry out tasks like this only.<\/p>\n We will use numpy.where()<\/em> to see the results.<\/p>\n <\/p>\n We have converted the two arrays in a single array by using the where function like evaluating based on conditions of less than 12 and greater than 12.<\/p>\n The first value came out low because the value in the array arr\u00a0<\/em>is smaller than 12. Similarly, the last values returned are high because the value in array\u00a0arr\u00a0<\/em>is greater than 12.<\/p>\n Let’s see how it worked.<\/p>\n Numpy.where()<\/em> contains three arguments.<\/p>\n The first argument is the numpy array which got converted to a bool array.<\/p>\n Then numpy.where() iterated over the bool array and for every True, it yields corresponding element from list 1 i.e. high_values and for every False, it yields corresponding element from 2nd list i.e. low_values i.e.<\/p>\n This is how we created a new array from the older arrays.<\/p>\n<\/div>\n In the above example, we have used single conditions. Here we will see the example with multiple conditions.<\/p>\n <\/p>\n We executed multiple conditions on the array arr<\/em> and it returned a bool value. Then numpy.where() iterated over the bool array and for every True it yields corresponding element from the first list and for every False it yields the corresponding element from the 2nd list. Then constructs a new array by the values selected from both the lists based on the result of multiple conditions on numpy array arr i.e.<\/p>\n Now, we will pass the different values and see what the array returns.<\/p>\n\n
Syntax of np.where()<\/h2>\n
numpy.<\/span>where<\/span>(<\/span>condition<\/span>[<\/span>, x, y<\/span>])<\/span><\/pre>\n
\n
\n
Using numpy.where() with single condition<\/h3>\n
arr = np.<\/span>array<\/span>([<\/span>11<\/span>, <\/span>12<\/span>, <\/span>13<\/span>, <\/span>14<\/span>])\r\n<\/span>high_values = [<\/span>'High'<\/span>, <\/span>'High'<\/span>, <\/span>'High'<\/span>, <\/span>'High'<\/span>]\r\n<\/span>low_values = [<\/span>'Low'<\/span>, <\/span>'Low'<\/span>, <\/span>'Low'<\/span>, <\/span>'Low'<\/span>]<\/span><\/pre>\n<\/div>\n
[<\/span>'Low'<\/span> 'Low'<\/span> 'High'<\/span> 'High'<\/span>]\r\n<\/span><\/pre>\n
arr = np.<\/span>array<\/span>([<\/span>11<\/span>, <\/span>12<\/span>, <\/span>13<\/span>, <\/span>14<\/span>])\r\n<\/span>high_values = [<\/span>'High'<\/span>, <\/span>'High'<\/span>, <\/span>'High'<\/span>, <\/span>'High'<\/span>]\r\n<\/span>low_values = [<\/span>'Low'<\/span>, <\/span>'Low'<\/span>, <\/span>'Low'<\/span>, <\/span>'Low'<\/span>]\r\n<\/span># numpy where() with condition argument\r\nresult = np.where<\/span>(<\/span>arr <\/span>><\/span> 12<\/span>,<\/span>['High'<\/span>, <\/span>'High'<\/span>, <\/span>'High'<\/span>, <\/span>'High'<\/span>]<\/span>,<\/span>['Low'<\/span>, <\/span>'Low'<\/span>, <\/span>'Low'<\/span>, <\/span>'Low'<\/span>])\r\n<\/span>print(<\/span>result<\/span>)<\/span><\/pre>\n<\/div>\n
arr <\/span>><\/span> 12<\/span> ==<\/span>><\/span> [<\/span>False<\/span> False<\/span> True<\/span> True<\/span>]<\/span><\/pre>\n
[<\/span>False<\/span> False<\/span> True<\/span> True<\/span>]<\/span> ==<\/span>><\/span> [<\/span>\u2018Low\u2019, \u2018Low\u2019, \u2018High\u2019, \u2018High\u2019<\/span>]<\/span><\/pre>\n
Using numpy.where() with multiple conditions<\/h3>\n
arr = np.<\/span>array<\/span>([<\/span>11<\/span>, <\/span>12<\/span>, <\/span>14<\/span>, <\/span>15<\/span>, <\/span>16<\/span>, <\/span>17<\/span>])\r\n<\/span># pass condition expression only\r\nresult = np.where<\/span>((<\/span>arr <\/span>><\/span> 12<\/span>)<\/span> & <\/span>(<\/span>arr <\/span><<\/span> 16<\/span>)<\/span>,<\/span>['A'<\/span>, <\/span>'A'<\/span>, <\/span>'A'<\/span>, <\/span>'A'<\/span>, <\/span>'A'<\/span>, <\/span>'A'<\/span>]<\/span>,<\/span>['B'<\/span>, <\/span>'B'<\/span>, <\/span>'B'<\/span>, <\/span>'B'<\/span>, <\/span>'B'<\/span>, <\/span>'B'<\/span>])\r\n<\/span>print(<\/span>result<\/span>)<\/span><\/pre>\n<\/div>\n
\n
arr = np.<\/span>array<\/span>([<\/span>11<\/span>, <\/span>12<\/span>, <\/span>14<\/span>, <\/span>15<\/span>, <\/span>16<\/span>, <\/span>17<\/span>])\r\n<\/span># pass condition expression only\r\nresult = np.where<\/span>((<\/span>arr <\/span>><\/span> 12<\/span>)<\/span> & <\/span>(<\/span>arr <\/span><<\/span> 16<\/span>)<\/span>,<\/span>['A'<\/span>, <\/span>'B'<\/span>, <\/span>'C'<\/span>, <\/span>