{"id":5555,"date":"2023-10-27T09:31:01","date_gmt":"2023-10-27T04:01:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=5555"},"modified":"2023-11-10T12:02:10","modified_gmt":"2023-11-10T06:32:10","slug":"python-numpy-select-elements-or-indices-by-conditions-from-numpy-array","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/","title":{"rendered":"Python Numpy : Select elements or indices by conditions from Numpy Array"},"content":{"rendered":"
In this article we will see how we can select and print elements from a given Numpy array provided with multiple conditions.<\/p>\n
When we apply a comparison operator to a numpy array it is applied to each & every elements of the array. It is seen that True or False will be returned as bool array accordingly when its elements satisfies the condition.<\/p>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\n# To create a numpy array from 2 to 25 with interval 23\r\nnum_arr = sc.arange(2, 25, 3)\r\n\r\n# To compare with all elements in array\r\nbool_arr = num_arr < 15\r\nprint(bool_arr)\r\n\r\n<\/pre>\nOutput :\r\n[ True\u00a0 True\u00a0 True\u00a0 True\u00a0 True False False False]<\/pre>\nIf we pass the resulted bool Numpy array in
[] operartor<\/code> then it will form a new Numpy Array with elements which were found True in corresponding bool Numpy array.<\/p>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\nnum_arr = sc.arange(2, 25, 3)\r\nbool_arr = num_arr < 15\r\nprint(bool_arr)\r\n\r\n# Those elements will be selected where it is True at corresponding value in bool array\r\nnew_arr = num_arr[bool_arr]\r\nprint(new_arr)<\/pre>\nOutput :\r\n[ True\u00a0 True\u00a0 True\u00a0 True\u00a0 True False False False]\r\n[ 2\u00a0 5\u00a0 8 11 14]<\/pre>\nSelect elements from Numpy Array which are divisible by 5 :<\/h3>\n
We can select and print those elements which are divisible by 5 from given Numpy array.<\/p>\n
<\/p>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\n# Numpy arrray with elements frrm 3 to 25\r\nnum_arr = sc.arange(3, 25, 1)\r\n\r\n# To select those numbers which are divisible by 5\r\nnew_arr = num_arr[num_arr%5==0]\r\n\r\nprint(new_arr)<\/pre>\nOutput :\r\n[ 5\u00a0 10\u00a0 15\u00a0 20 ]<\/pre>\nSelect elements from Numpy Array which are greater than 10 and less than 18 :<\/h3>\n
We can select and print those elements which are smaller than 10 and greater than 18 from given Numpy array.<\/p>\n
<\/p>\n
#Program :\r\n\r\nimport numpy as sc\r\n\r\n# Numpy arrray with elements frrm 3 to 25\r\nnum_arr = sc.arange(3, 25, 1)\r\n\r\n# To select those numbers which are greater than 10 and smaller than 18\r\nnew_arr = num_arr[(num_arr > 10) & (num_arr < 18)]\r\n\r\nprint(new_arr)<\/pre>\nOutput :\r\n[11 12 13 14 15 16 17]<\/pre>\n<\/p>\n","protected":false},"excerpt":{"rendered":"
How to elect elements or indices by conditions from Numpy Array in Python ? In this article we will see how we can select and print elements from a given Numpy array provided with multiple conditions. Selecting elements from a Numpy array based on Single or Multiple Conditions : When we apply a comparison operator …<\/p>\n