{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/sort-a-pandas-dataframe-using-the-sort-values-method/",
    "result": {"data":{"markdownRemark":{"html":"<p>Let’s read an Excel file into a DataFrame:</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk10\">import</span><span class=\"mtk15\"> pandas </span><span class=\"mtk10\">as</span><span class=\"mtk15\"> pd</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> pd.</span><span class=\"mtk6\">read_excel</span><span class=\"mtk15\">(</span><span class=\"mtk16\">&quot;budget.xlsx&quot;</span><span class=\"mtk15\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"4\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"5\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 571px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/737e89184d274ef888bd11d6832ed802/503dd/budget.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 69%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAIAAACgpqunAAAACXBIWXMAAA7DAAAOwwHHb6hkAAACBUlEQVQoz1WS6W7bMBCE8/6Pl6Y1AviQRJESKR7iteLNwHba1PN/8O3M7FvaZdhojbG15t1dpZTeewhHCMFZG2LQWmtjAMB7r7W21jprc85vvbfee22Ncz7P85/TCWNCKT2fz+tKT6fTOI4IoY/fH5+fnxjPwzheL5df7+/amLf+UGtNqT3njOdZSokxvtxuIcZtY8aYWitldFlpfgjgGG6D9/7HbB+XTNNkjOV8G8YhxKiksNbWWvddreuSc045HQDXy/XF/Ey7rqv3AAAYz6VUa7T3vrWm952yrZSSc44hkHkGOH7MWuuUIsazEIIxhjFhG8MYO+dyzpzzdaUppRhjCMeMZu/h29x7d87VWtnGpFIIIYwxwCGFAPC9d7UrxrZaaynlzpjx8T9Z7XuMESEklSLLchmuIUQphDb6ScaEPLABAG63m3Puh+y9r60uZLHOp5SWhaSU7WPe3ru1llL6TY5xGqeXzEqpGOOEJiEVIXgaxyOEjVGtdU5ZKbGsy5Psvb9czi9tG6PvmRndtcYYTwjlUhhjzrl7ZqXYxp/kO2McAP4WVkvmXDyuXYy5/x9CKMSo9f7cn3O+bfzZNgCMwwj/2q61CilLKYQQbTTBM8bEe+APcmtNCE6W5blzCOHlw2op+65TSoJz57xSatsY55wxBgA5ZamkEDLdFb2HhZDjCF8D4Brr0yeWWgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Budget\"\n        title=\"Budget\"\n        src=\"/static/737e89184d274ef888bd11d6832ed802/503dd/budget.png\"\n        srcset=\"/static/737e89184d274ef888bd11d6832ed802/56d15/budget.png 200w,\n/static/737e89184d274ef888bd11d6832ed802/d9f49/budget.png 400w,\n/static/737e89184d274ef888bd11d6832ed802/503dd/budget.png 571w\"\n        sizes=\"(max-width: 571px) 100vw, 571px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Let’s say we want to sort the <del>July’19 Budget</del> column.</p>\n<p>We can sort the column using the <del>sort_values</del> method.</p>\n<p>We pass the name of the column we want to sort to the <del>by</del> parameter. And we pass <del>inplace = True</del> to make permanent changes in the <del>budget</del> DataFrame.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget.</span><span class=\"mtk6\">sort_values</span><span class=\"mtk15\">(</span><span class=\"mtk8 mtki\">by</span><span class=\"mtk15\"> </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> </span><span class=\"mtk16\">&quot;July&#39;19 Budget&quot;</span><span class=\"mtk15\">,  </span><span class=\"mtk8 mtki\">inplace</span><span class=\"mtk15\"> </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> </span><span class=\"mtk5\">True</span><span class=\"mtk15\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget.</span><span class=\"mtk6\">head</span><span class=\"mtk15\">()</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 559px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/8a51f77204f28c738e0f9c02ac514ea2/11d18/budgetSorted.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 32.49999999999999%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABMUlEQVQY00WQXW+DIBiF+///1y52tXRbP3RTUaBdVVQQeF8EZLFt0nN1cpIneXJ2i2j9NKaUvPfWWkSM65pSWhb3WJxzFqy11lhYFm/MvRuDCLs1hrSuiMgZy7Ls6/sghqHruvP5zBk/HU8VqX6LMjt8fry/NVWR5/nPT348nvb7/S7do7bMAEBIrZSiDS2qCp2bhJiVijE6PdWkMqPw3ocQtNZlWT7hTQXAOffXtkaby/XKL5cQohwno01cV6clYxTmKQQfYzDGcM5esFIKESjlgxBFWTSUIaLoOynlsng79TWp5rFHROeclLKuyRMGgMdbt7ZTSpGmJqQJMba3m5RyTWnTpg3MMoYQYzRa16R+wVrrWc+ccURUUjHOAXEUw2bkFjN2tMj02AMAIkopGWX/H0KKCf0ol4MAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Budget\"\n        title=\"Budget\"\n        src=\"/static/8a51f77204f28c738e0f9c02ac514ea2/11d18/budgetSorted.png\"\n        srcset=\"/static/8a51f77204f28c738e0f9c02ac514ea2/56d15/budgetSorted.png 200w,\n/static/8a51f77204f28c738e0f9c02ac514ea2/d9f49/budgetSorted.png 400w,\n/static/8a51f77204f28c738e0f9c02ac514ea2/11d18/budgetSorted.png 559w\"\n        sizes=\"(max-width: 559px) 100vw, 559px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>By default, the <del>sort_values()</del> method sorts the values in ascending order. If we want to sort in descending order, then we pass a value of <del>False</del> to the <del>ascending</del> parameter.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget.</span><span class=\"mtk6\">sort_values</span><span class=\"mtk15\">(</span><span class=\"mtk8 mtki\">by</span><span class=\"mtk15\"> </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> </span><span class=\"mtk16\">&quot;July&#39;19 Budget&quot;</span><span class=\"mtk15\">, </span><span class=\"mtk8 mtki\">ascending</span><span class=\"mtk15\"> </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> </span><span class=\"mtk5\">False</span><span class=\"mtk15\">, </span><span class=\"mtk8 mtki\">inplace</span><span class=\"mtk15\"> </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> </span><span class=\"mtk5\">True</span><span class=\"mtk15\">)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget.</span><span class=\"mtk6\">head</span><span class=\"mtk15\">()</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 561px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/0f3c782c973742dd993b6c35d99fa60f/66712/budgetSortedDescending.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 32%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA+ElEQVQY01WPUW/DIBCD+/9/YKZ2D004AhzhSJoGOBIgWzVN3SxZ8ssn25eSYtnWynwch7VWgiSin+yc01pPzgkhBiEMotYaBEgphRAxxsv5K0Qk8mIYAGCZ5+vtOs+zFGAQPbnh1vWfHbmJiBBN99Ety/KGY4ytNSLyfl4fa9/fW2vr48Gcz1YXZxzqs5bzPDMnAPjXTESJ2U2TnV5Wo0J8vQghZk6TllqKzJFz3rb13vfbFt5wCKGUMntvENEggNyPHQ1uIbRaCEdUUMtRat1zBilj+tP8fD5TSkSkjVFay3G01qpxDCHumb0BC/fMiZlTSkqp79lfSpxRfSXmu7oAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Budget\"\n        title=\"Budget\"\n        src=\"/static/0f3c782c973742dd993b6c35d99fa60f/66712/budgetSortedDescending.png\"\n        srcset=\"/static/0f3c782c973742dd993b6c35d99fa60f/56d15/budgetSortedDescending.png 200w,\n/static/0f3c782c973742dd993b6c35d99fa60f/d9f49/budgetSortedDescending.png 400w,\n/static/0f3c782c973742dd993b6c35d99fa60f/66712/budgetSortedDescending.png 561w\"\n        sizes=\"(max-width: 561px) 100vw, 561px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .synthwave-84 { background-color: #262335; }\n  .synthwave-84 .mtki { font-style: italic; }\n  .synthwave-84 .mtk10 { color: #FEDE5D; }\n  .synthwave-84 .mtk15 { color: #FF7EDBFF; }\n  .synthwave-84 .mtk12 { color: #FFFFFFEE; }\n  .synthwave-84 .mtk6 { color: #36F9F6; }\n  .synthwave-84 .mtk16 { color: #FF8B39; }\n  .synthwave-84 .mtk8 { color: #72F1B8; }\n  .synthwave-84 .mtk5 { color: #F97E72; }\n  .synthwave-84 .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(255, 255, 255, 0.1));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(255, 255, 255, 0.5));\n  }\n</style>","frontmatter":{"title":"Sort a Pandas DataFrame Using the sort.values() Method","date":"2021-08-07"}}},"pageContext":{"slug":"/sort-a-pandas-dataframe-using-the-sort-values-method/","prev":{"fields":{"slug":"/ranking-values-using-the-rank-method-in-pandas/"},"frontmatter":{"modules":null}},"next":{"fields":{"slug":"/renaming-columns-in-a-dataframe-in-pandas/"},"frontmatter":{"modules":null}}}},
    "staticQueryHashes": ["3159585216"]}