{"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":"

How to elect elements or indices by conditions from Numpy Array in Python ?<\/h2>\n

In this article we will see how we can select and print elements from a given Numpy array provided with multiple conditions.<\/p>\n

Selecting elements from a Numpy array based on Single or Multiple Conditions :<\/h3>\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>\n
Output :\r\n[ True\u00a0 True\u00a0 True\u00a0 True\u00a0 True False False False]<\/pre>\n

If 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>\n
Output :\r\n[ True\u00a0 True\u00a0 True\u00a0 True\u00a0 True False False False]\r\n[ 2\u00a0 5\u00a0 8 11 14]<\/pre>\n

Select 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>\n
Output :\r\n[ 5\u00a0 10\u00a0 15\u00a0 20 ]<\/pre>\n

Select 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>\n
Output :\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

Python Numpy : Select elements or indices by conditions from Numpy Array<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Numpy : Select elements or indices by conditions from Numpy Array - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Numpy : Select elements or indices by conditions from Numpy Array - Python Programs\" \/>\n<meta property=\"og:description\" content=\"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 … Python Numpy : Select elements or indices by conditions from Numpy Array Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-27T04:01:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:32:10+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/\",\"name\":\"Python Numpy : Select elements or indices by conditions from Numpy Array - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-27T04:01:01+00:00\",\"dateModified\":\"2023-11-10T06:32:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Numpy : Select elements or indices by conditions from Numpy Array\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Python Numpy : Select elements or indices by conditions from Numpy Array\",\"datePublished\":\"2023-10-27T04:01:01+00:00\",\"dateModified\":\"2023-11-10T06:32:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#webpage\"},\"wordCount\":184,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Numpy : Select elements or indices by conditions from Numpy Array - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/","og_locale":"en_US","og_type":"article","og_title":"Python Numpy : Select elements or indices by conditions from Numpy Array - Python Programs","og_description":"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 … Python Numpy : Select elements or indices by conditions from Numpy Array Read More »","og_url":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-27T04:01:01+00:00","article_modified_time":"2023-11-10T06:32:10+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#webpage","url":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/","name":"Python Numpy : Select elements or indices by conditions from Numpy Array - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-27T04:01:01+00:00","dateModified":"2023-11-10T06:32:10+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Numpy : Select elements or indices by conditions from Numpy Array"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Python Numpy : Select elements or indices by conditions from Numpy Array","datePublished":"2023-10-27T04:01:01+00:00","dateModified":"2023-11-10T06:32:10+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#webpage"},"wordCount":184,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-numpy-select-elements-or-indices-by-conditions-from-numpy-array\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/5555"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=5555"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/5555\/revisions"}],"predecessor-version":[{"id":5557,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/5555\/revisions\/5557"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=5555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=5555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=5555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}