{"id":13580,"date":"2021-03-31T16:35:09","date_gmt":"2021-03-31T16:35:09","guid":{"rendered":"https:\/\/www.askpython.com\/?p=13580"},"modified":"2021-03-31T16:35:12","modified_gmt":"2021-03-31T16:35:12","slug":"sorting-techniques-in-numpy","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/sorting-techniques-in-numpy","title":{"rendered":"3 Easy Sorting Techniques in NumPy"},"content":{"rendered":"\n<p>Hello, readers! In this article, we will be focusing on <strong>3 Sorting techniques in NumPy<\/strong>, in detail.<\/p>\n\n\n\n<p>So, let us begin! \ud83d\ude42<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-module\" class=\"rank-math-link\">Python NumPy library<\/a><\/strong> offers us various functions to create an array and manipulate elements of a similar type in an array structure. Along with this, NumPy offers us various functions that can enable us to sort the elements present in the array structure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sorting Techniques in NumPy<\/h2>\n\n\n\n<p>We&#8217;ll learn the below sorting techniques in NumPy.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>NumPy sort() function<\/strong><\/li><li><strong>NumPy argsort() function<\/strong><\/li><li><strong>NumPy lexsort() function<\/strong><\/li><\/ol>\n\n\n\n<p>So, let us begin!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-2\"><\/span><span id=\"eh-1\"><\/span>1. NumPy sort() function<\/h3>\n\n\n\n<p>In order to sort the various elements present in the array structure, NumPy provides us with <strong>sort()<\/strong> function. With sort() function, we can sort the elements and segregate them in ascending to descending order, respectively.<\/p>\n\n\n\n<p>Have a look at the below syntax!<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.sort(array, axis)\n<\/pre><\/div>\n\n\n<p>The parameter &#8216;axis&#8217; specifies the manner in which sorting needs to be performed. So when we set axis = NONE, the sorting occurs in the traditional fashion, and the resultant array is a single row of elements. On the other hand, if we set axis = 1, the sorting occurs in a row-wise fashion, that is each and every row gets sorted individually.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<p>In this example, we have created an array, further we have sorted the array using the <a href=\"https:\/\/www.askpython.com\/python\/list\/python-sort-list\" class=\"rank-math-link\">sort() function<\/a> and with <strong>axis = NONE<\/strong> i.e. it sorts the elements in ascending order.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\ndata = np.array(&#x5B;&#x5B;22, 55], &#x5B;0, 10]])\nres = np.sort(data, axis = None)        \nprint (&quot;Data before sorting:&quot;, data)\nprint(&quot;Data after sorting:&quot;, res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nData before sorting: &#x5B;&#x5B;22 55]\n &#x5B; 0 10]]\nData after sorting: &#x5B; 0 10 22 55]\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<p>In this example, we have created an array and sorted the same using sort() function, here we have set axis = 1 i.e. row wise sorting has been performed.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\ndata = np.array(&#x5B;&#x5B;66, 55, 22], &#x5B;0, 10, -1]])\nres = np.sort(data, axis = 1)        \nprint (&quot;Data before sorting:&quot;, data)\nprint(&quot;Row wise sorting:&quot;, res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nData before sorting: &#x5B;&#x5B;66 55 22]\n &#x5B; 0 10 -1]]\nRow wise sorting: &#x5B;&#x5B;22 55 66]\n &#x5B;-1  0 10]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-3\"><\/span><span id=\"eh-2\"><\/span>2. NumPy argsort()<\/h3>\n\n\n\n<p>Apart from the sort() method, we also have <strong>argsort()<\/strong> function that is used as a sorting techniques in NumPy which returns an <strong>array of indices<\/strong> of the sorted elements. From those sorted index values, we can get the sorted array elements in ascending order.<\/p>\n\n\n\n<p>Thus, with argsort() function, we can sort the array values and get the index values of the same as a separate array.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\ndata = np.array(&#x5B;66, 55, 22,11,-1,0,10])\nres_index = np.argsort(data)        \nprint (&quot;Data before sorting:&quot;, data)\nprint(&quot;Sorted index values of the array:&quot;, res_index)\n\nx = np.zeros(len(res_index), dtype = int)\nfor i in range(0, len(x)):\n    x&#x5B;i]= data&#x5B;res_index&#x5B;i]]\nprint(&#039;Sorted array from indexes:&#039;, x)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>In the above example, we have performed argsort() function on the data values and have obtained sorted index values of the elements. Further, we have utilized the same array index values to get the sorted array elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nData before sorting: &#x5B;66 55 22 11 -1  0 10]\nSorted index values of the array: &#x5B;4 5 6 3 2 1 0]\nSorted array from indexes: &#x5B;-1  0 10 11 22 55 66]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-4\"><\/span><span id=\"eh-3\"><\/span>3. NumPy lexsort() function<\/h3>\n\n\n\n<p>The lexsort() function enables us to sort the data values using sequence of keys i.e. by columns. With <strong>lexsort()<\/strong> function, we sort the two arrays taking one at a time into consideration. As a result, we get the index values of the sorted elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\ndata = np.array(&#x5B;66, 55, 22,11,-1,0,10])\ndata1 = np.array(&#x5B;1,2,3,4,5,0,-1])\nres_index = np.lexsort((data1, data))        \nprint(&quot;Sorted index values of the array:&quot;, res_index)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSorted index values of the array: &#x5B;4 5 6 3 2 1 0]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Feel free to comment below, in case you come across any question. For more such posts related to Python programming, Stay tuned with us.<\/p>\n\n\n\n<p>Till then, Happy Learning!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, readers! In this article, we will be focusing on 3 Sorting techniques in NumPy, in detail. So, let us begin! \ud83d\ude42 Python NumPy library offers us various functions to create an array and manipulate elements of a similar type in an array structure. Along with this, NumPy offers us various functions that can enable [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":14423,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93,1],"tags":[],"class_list":["post-13580","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=13580"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13580\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/14423"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=13580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=13580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=13580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}