{"id":672,"date":"2023-11-29T07:20:05","date_gmt":"2023-11-29T07:20:05","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=672"},"modified":"2023-12-01T07:29:55","modified_gmt":"2023-12-01T07:29:55","slug":"pandas-sort-by-columnns-sort-values","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/pandas-sort-by-columnns-sort-values\/","title":{"rendered":"Pandas DataFrame sort_values() | Sort by Column"},"content":{"rendered":"\n<p>Sorting data is an essential task in data analysis, and Pandas provides a powerful method called sort_values() to sort DataFrames based on one or more columns. In this article, we will learn how to use sort_value method to Sort by Column a Pandas DataFrame and with the different parameters that can be used to customize the sorting behavior.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the&nbsp;sort_values() Function in Pandas?<\/strong><\/h2>\n\n\n\n<p><strong>The sort_values() function in Pandas allows us to sort DataFrames on the basis of one or more columns.<\/strong> It takes several parameters that can define the sorting behavior, such as: the columns to sort by, the sorting order (ascending or descending), and handling null values.<\/p>\n\n\n\n<p>Let us now explore the various ways to use the sort_values function to sort a DataFrame based on single and multiple columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sorting in Ascending Order<\/strong><\/h2>\n\n\n\n<p>Sorting data in a DataFrame in ascending order is the default behavior of the sort_values() function. This means that when no sorting order is defined, the sort_values will sort the DataFrame in Ascending Order.\u00a0<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Sorting by a Single Column&nbsp;<\/strong><\/h3>\n\n\n\n<p><strong>To sort a DataFrame by a single column, we can simply pass the column name to the by parameter of the sort_values() function.<\/strong>&nbsp;<\/p>\n\n\n\n<p>For example, let&#8217;s sort a DataFrame called df by the &#8216;Age\u2019&#8217; column with implementation in Python:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\ndata = {\n    &quot;Name&quot;: [&quot;John&quot;, &quot;Emma&quot;, &quot;Michael&quot;],\n    &quot;Age&quot;: [45, 30, 35],\n    &quot;City&quot;: [&quot;New York&quot;, &quot;London&quot;, &quot;Paris&quot;]\n}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Sort the DataFrame by the 'Age' column\ndf = df.sort_values(by='Age')\n\n# Display the sorted DataFrame\nprint('Sorted DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n       Name  Age      City\n0     John   45  New York\n1     Emma   30    London\n2  Michael   35     Paris\n\nSorted DataFrame:\n       Name  Age      City\n1     Emma   30    London\n2  Michael   35     Paris\n0     John   45  New York\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Sorting by Multiple Columns<\/strong>&nbsp;<\/h3>\n\n\n\n<p>Sorting by multiple columns allows us to define a hierarchical sorting order. We can pass a list of column names to the by parameter to sort the DataFrame based on multiple columns. The sorting is performed sequentially, with the first column taking precedence over the second, and so on. This simply means, in the case of clashes in the first column, the second column will be used as a second preference.<\/p>\n\n\n\n<p>Let us consider an example, let&#8217;s sort the DataFrame df by the &#8216;Age\u2019 and &#8216;Name&#8217; columns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\ndata = {\n    &quot;Name&quot;: [&quot;John&quot;, &quot;Zmma&quot;, &quot;Michael&quot;],\n    &quot;Age&quot;: [45, 30, 35],\n    &quot;City&quot;: [&quot;New York&quot;, &quot;London&quot;, &quot;Paris&quot;]\n}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Sort the DataFrame by multiple columns ('Age' and 'Name')\ndf = df.sort_values(by=['Age', 'Name'])\n\n# Display the sorted DataFrame\nprint('Sorted DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n       Name  Age      City\n0     John   45  New York\n1     Zmma   30    London\n2  Michael   35     Paris\n\nSorted DataFrame:\n       Name  Age      City\n1     Zmma   30    London\n2  Michael   35     Paris\n0     John   45  New York\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Sorting with Null Values&nbsp;<\/strong><\/h3>\n\n\n\n<p>By default, the sort_values() function places null values at the end of the sorted DataFrame. However, we can change this behavior by setting the na_position parameter to &#8216;first&#8217; to place null values at the beginning.&nbsp;<\/p>\n\n\n\n<p>For example, let us try to sort the DataFrame df by the &#8216;Age\u2019&#8217; column with null values first:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\ndata = {\n    &quot;Name&quot;: [&quot;John&quot;, &quot;Emma&quot;, &quot;Michael&quot;],\n    &quot;Age&quot;: [45, 30, None],\n    &quot;City&quot;: [&quot;New York&quot;, &quot;London&quot;, &quot;Paris&quot;]\n}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Sort the DataFrame by column ('Age') with null values placed first\ndf = df.sort_values(by='Age', na_position='first')\n\n# Display the sorted DataFrame\nprint('Sorted DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n       Name   Age      City\n0     John  45.0  New York\n1     Emma  30.0    London\n2  Michael   NaN     Paris\n\nSorted DataFrame:\n       Name   Age      City\n2  Michael   NaN     Paris\n1     Emma  30.0    London\n0     John  45.0  New York\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sorting in Descending Order&nbsp;<\/strong><\/h2>\n\n\n\n<p>We can also sort the DataFrame in similar ways in Descending Order. To do this we simply need to set the ascending parameter to False. This will reverse the sorting order of the specified column(s).<\/p>\n\n\n\n<p>Let us try a simple example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\ndata = {\n    &quot;Name&quot;: [&quot;John&quot;, &quot;Emma&quot;, &quot;Michael&quot;],\n    &quot;Age&quot;: [45, 30, 55],\n    &quot;City&quot;: [&quot;New York&quot;, &quot;London&quot;, &quot;Paris&quot;]\n}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Sort the DataFrame by column ('Age') in descending order\ndf = df.sort_values(by='Age', ascending=False)\n\n# Display the sorted DataFrame\nprint('Sorted DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n       Name  Age      City\n0     John   45  New York\n1     Emma   30    London\n2  Michael   55     Paris\n\nSorted DataFrame:\n       Name  Age      City\n2  Michael   55     Paris\n0     John   45  New York\n1     Emma   30    London\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sorting with Custom Sorting Algorithm<\/strong><\/h2>\n\n\n\n<p>We can also use custom sorting algorithms to sort a DataFrame. <strong>The sort_values() function in Pandas provides different sorting algorithms to choose from. By default, the sorting algorithm used for the sort_values function is&nbsp; &#8216;quicksort&#8217;. <\/strong>We can change this to any algorithm according to our needs.&nbsp;<\/p>\n\n\n\n<p>For example, let us try to sort a DataFrame using the \u2018merge sort\u2019:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\ndata = {\n    &quot;Name&quot;: [&quot;John&quot;, &quot;Emma&quot;, &quot;Michael&quot;],\n    &quot;Age&quot;: [45, 30, 55],\n    &quot;City&quot;: [&quot;New York&quot;, &quot;London&quot;, &quot;Paris&quot;]\n}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Sort the DataFrame by column ('Age') using mergesort\ndf = df.sort_values(by='Age', kind='mergesort')\n\n# Display the sorted DataFrame\nprint('Sorted DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n       Name  Age      City\n0     John   45  New York\n1     Emma   30    London\n2  Michael   55     Paris\n\nSorted DataFrame:\n       Name  Age      City\n1     Emma   30    London\n0     John   45  New York\n2  Michael   55     Paris<\/code><\/pre>\n\n\n\n<p>You should also now learn how to <a href=\"https:\/\/favtutor.com\/articles\/rename-column-pandas\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/rename-column-pandas\/\">rename columns in Pandas<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have explored the different ways and parameters used to sort the DataFrames on the basis of single or multiple columns. By mastering the techniques discussed in this article, we can manipulate and analyze data more effectively using Pandas.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Need to sort Pandas by Column? Learn about  sort_values() Method in Pandas DataFrame with Examples.<\/p>\n","protected":false},"author":10,"featured_media":675,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[35],"tags":[37,53],"class_list":["post-672","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-pandas","tag-pandas-dataframes"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=672"}],"version-history":[{"count":3,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/672\/revisions"}],"predecessor-version":[{"id":707,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/672\/revisions\/707"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/675"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}