

{"id":79326,"date":"2020-07-25T14:29:56","date_gmt":"2020-07-25T08:59:56","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=79326"},"modified":"2021-05-09T13:13:36","modified_gmt":"2021-05-09T07:43:36","slug":"numpy-search-sort-and-count","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/","title":{"rendered":"NumPy Sort, Search and Count Functions"},"content":{"rendered":"<p>NumPy contains functions to perform search, sort, and count operations. There are a wide variety of functions to get the output. We can use the functions accordingly to produce the most optimum output. We can implement these functions on the array object.<\/p>\n<p>The functions can be used along with each other on the same data. We use NumPy search to find a particular element from the array. We use Numpy sort function for arranging the array in a particular order. NumPy counting function returns the count of a particular value.<\/p>\n<h2>NumPy Searching Function<\/h2>\n<p>We perform NumPy search operation to determine the position of a given element or value inside an array. There are functions to find the maximum, minimum, or a value satisfying a particular condition. The search turns out to be successful if the value is found.<\/p>\n<h3>1. np.argmax()<\/h3>\n<p>This function returns the index of the maximum value in the array list. The search can be specific to a particular axis or the entire array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr=np.array([[74,23],[56,98]])\r\nprint(np.argmax(arr))<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">3<\/div>\n<h3>2. np.nanargmax()<\/h3>\n<p>This function returns the index of the maximum value element after ignoring \u2018Not a Number\u2019 (NaN) values present in the list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr=np.array([[74,np.nan,59],[56,98,np.nan]])\r\nprint(np.nanargmax(arr))<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">4<\/div>\n<h3>3. np.argmin()<\/h3>\n<p>This function returns the index of the minimum value in the array list. The search can be specific to a particular axis or the entire array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr=np.array([[74,59],[56,98]])\r\nprint(np.argmin(arr))\r\n<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">2<\/div>\n<h3>4. np.nanargmin()<\/h3>\n<p>This function returns the index of the minimum value element after ignoring \u2018Not a Number\u2019 (NaN) values present in the list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr=np.array([[74,np.nan,59],[56,98,np.nan]])\r\nprint(np.nanargmin(arr))<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">3<\/div>\n<h3>5. np.extract()<\/h3>\n<p>This function returns the array values that satisfy a particular condition.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr=np.array([[74,33],[59,98]])\r\na=np.mod(arr,2) ==0\r\nprint(np.extract(a,arr))\r\n<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">[74 98]<\/div>\n<h3>6. np.nonzero()<\/h3>\n<p>This function search returns values that are non-zero.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr=np.array([0,56,89,0,23])\r\nprint(np.nonzero(arr))\r\n\r\n<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">(array([1, 2, 4]))<\/div>\n<table>\n<tbody>\n<tr>\n<td>Function<\/td>\n<td>Description<\/td>\n<\/tr>\n<tr>\n<td>np.argwhere()<\/td>\n<td>It is used to group elements having non-zero indices.<\/td>\n<\/tr>\n<tr>\n<td>np.flatnonzero()<\/td>\n<td>It returns a flattened array consisting of elements with non-zero indices.<\/td>\n<\/tr>\n<tr>\n<td>np.where()<\/td>\n<td>It returns elements following a condition.<\/td>\n<\/tr>\n<tr>\n<td>np.searchsorted()<\/td>\n<td>It returns indices of elements that are in sorted manner.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>NumPy Sorting Function<\/h2>\n<p>There are a wide variety of sorting functions in NumPy. These NumPy Sort functions arrange the data in a particular order. We choose the best sorting algorithm depending on the output criteria. We can apply for any order over the data. There are various sorting functions available.<\/p>\n<h3>1. np.sort()<\/h3>\n<p>This function returns an array in sorted format.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr = np.array([[16,1,47,53,87,28]])\r\nprint(np.sort(arr))\r\n \r\n<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">[[ 1 16 28 47 53 87]]<\/div>\n<h3>2. np.argsort()<\/h3>\n<p>This function returns the indices of the sorted array. We can perform this along a particular axis.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr = np.array([[16,1,47,53,87,28]])\r\nprint(np.argsort(arr))<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">[[1 0 5 2 3 4]]<\/div>\n<h3>3. np.lexsort()<\/h3>\n<p>This function returns the indices of array elements indirectly sorted by using a key sequence.We can consider the last key as the primary one.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr1 =np.array([[16,1,47,53,87,28]])\r\narr2 =np.array([[56,90,44,32,77,45]])\r\n#sort by arr1 and then arr2\r\nprint(np.lexsort((arr2,arr1)))\r\n<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">[[1 0 5 2 3 4]]<\/div>\n<table>\n<tbody>\n<tr>\n<td>Function<\/td>\n<td>Description<\/td>\n<\/tr>\n<tr>\n<td>np.ndarraysort()<\/td>\n<td>It is for sorting an array in place.<\/td>\n<\/tr>\n<tr>\n<td>np.msort()<\/td>\n<td>It returns an array which sorted along the first axis<\/td>\n<\/tr>\n<tr>\n<td>np.partition()<\/td>\n<td>It returns a copy of array partition.<\/td>\n<\/tr>\n<tr>\n<td>np.argpartition()<\/td>\n<td>It returns the partition of an aray along a specific axis.<\/td>\n<\/tr>\n<tr>\n<td>np.sort_compex()<\/td>\n<td>It sorts the real and imaginary parts separately.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>NumPy Counting Function<\/h2>\n<p>We can apply NumPy Count to count a specific kind of value from the array list.<\/p>\n<h3>1. np.count_nonzero()<\/h3>\n<p>This function returns the count of all the non-zero values from the array. We can apply this function along a specific axis.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr=np.array([[16,0,56],[2,34,0],[90,87,0]])\r\nprint(np.count_nonzero(arr))\r\nprint(np.count_nonzero(arr,axis=0))\r\n \r\n<\/pre>\n<p>Output<\/p>\n<div class=\"code-output\">6<br \/>\n[3 2 1]<\/div>\n<h2>Summary<\/h2>\n<p>There are functions to perform searching, sorting, and counting on an array list in NumPy. These NumPy functions help in arranging the data according to the requirement. This is also useful to extract particular data in a sorted manner. It is also useful for further use of the sorted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy contains functions to perform search, sort, and count operations. There are a wide variety of functions to get the output. We can use the functions accordingly to produce the most optimum output. We&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":79789,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22401],"tags":[22755,22757,22753,22756,22754],"class_list":["post-79326","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-numpy-counting","tag-numpy-search-functions","tag-numpy-searching","tag-numpy-sort-function","tag-numpy-sorting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>NumPy Sort, Search and Count Functions - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about Numpy sort, search and count functions with examples. Learn Functions in each of these operations &amp; their uses in Numpy with syntax and example\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy Sort, Search and Count Functions - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about Numpy sort, search and count functions with examples. Learn Functions in each of these operations &amp; their uses in Numpy with syntax and example\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-25T08:59:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:43:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-searching-sorting-counting.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"NumPy Sort, Search and Count Functions - DataFlair","description":"Learn about Numpy sort, search and count functions with examples. Learn Functions in each of these operations & their uses in Numpy with syntax and example","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:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/","og_locale":"en_US","og_type":"article","og_title":"NumPy Sort, Search and Count Functions - DataFlair","og_description":"Learn about Numpy sort, search and count functions with examples. Learn Functions in each of these operations & their uses in Numpy with syntax and example","og_url":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-07-25T08:59:56+00:00","article_modified_time":"2021-05-09T07:43:36+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-searching-sorting-counting.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"NumPy Sort, Search and Count Functions","datePublished":"2020-07-25T08:59:56+00:00","dateModified":"2021-05-09T07:43:36+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/"},"wordCount":583,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-searching-sorting-counting.jpg","keywords":["numpy counting","numpy search functions","NumPy searching","numpy sort function","numpy sorting"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/","url":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/","name":"NumPy Sort, Search and Count Functions - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-searching-sorting-counting.jpg","datePublished":"2020-07-25T08:59:56+00:00","dateModified":"2021-05-09T07:43:36+00:00","description":"Learn about Numpy sort, search and count functions with examples. Learn Functions in each of these operations & their uses in Numpy with syntax and example","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-searching-sorting-counting.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-searching-sorting-counting.jpg","width":1200,"height":628,"caption":"NumPy Sort, Search and Count functions"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/numpy-search-sort-and-count\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"NumPy Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/numpy\/"},{"@type":"ListItem","position":3,"name":"NumPy Sort, Search and Count Functions"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=79326"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79326\/revisions"}],"predecessor-version":[{"id":93069,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79326\/revisions\/93069"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/79789"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=79326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=79326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=79326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}