{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/pandas-nlargest-and-nsmallest-methods/",
    "result": {"data":{"markdownRemark":{"html":"<div class=\"table-of-contents\">\n<ul>\n<li><a href=\"#nlargest\">nlargest()</a></li>\n<li><a href=\"#nsmallest\">nsmallest()</a></li>\n</ul>\n</div>\n<h3 id=\"nlargest\" style=\"position:relative;\"><a href=\"#nlargest\" aria-label=\"nlargest permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>nlargest()</h3>\n<p>Let’s read the <del>budget.xlsx</del> workbook 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: 556px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/cf82d2010d5097bfa04b2b182e0c513a/68b51/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: 70.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAIAAACgpqunAAAACXBIWXMAAA7DAAAOwwHHb6hkAAACHElEQVQoz0WS266kIBBF+/9/biY58zCd7oOigoIKFLcCRJ1pu5Oz3nfWrsq+baCKUed55pSMMT6E4zhq3SIixugvtNbaaEQEAKW1McY6u+/H7byIMXLOCSF/73chRNs0z+f3/aIfBtp1v37/btu2H3pK6ePx+Pr6k3K5ncdxnidahVYnZxglXi/t8y54D4tYp2HPsaJnHYFFHBmPHL1eBvLcMHzM6EyM0Tvf90POpeuHUcpSyrLMOeeUEuPcaCgXSum2bbfobuf5MierkrdoDe/olkJPGzHxDYOax5rihmFknVXrnnHPCOvcNc+fMAbvrXFG97TR69x+Pzjr+dBNvE8YUvSs7/QiM4aMwRlFCSk5f2qnlDHlshUhpTaGNM2yKkyolKr7XvdDSAHWHue5HwcicsZrrZ+HhRCcc947Sql3/puQcZoS4jyLdMHYsK5rKSXnDACkaUopH3O+QETG+L7vALCua61Va1Uv2MittftFCL6l9DJfhBCd8865jnYApuuolNI5N8uXOecspVBKvR0AhhCybfVnJBhjQpSzNABN20xCCiHnWe77XmsdpwkAjuNl9u513St8XDdba40xwfuBMUQcRz5OUyl5Web3zXzkWpuP2UBDmrJtn/B7wzHGSUwWoG3aeVmstcs8bxeMMa31u4VzjrbtdoXftV/fjjHIeY4hCCHWdZkmodY1vQaWhBAA8F4bAHA+/n/YP6RIF2ZPOAc/AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Budget\"\n        title=\"Budget\"\n        src=\"/static/cf82d2010d5097bfa04b2b182e0c513a/68b51/budget.png\"\n        srcset=\"/static/cf82d2010d5097bfa04b2b182e0c513a/56d15/budget.png 200w,\n/static/cf82d2010d5097bfa04b2b182e0c513a/d9f49/budget.png 400w,\n/static/cf82d2010d5097bfa04b2b182e0c513a/68b51/budget.png 556w\"\n        sizes=\"(max-width: 556px) 100vw, 556px\"\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>Our DataFrame has duplicate rows. We will remove them using the <del>drop_duplicates()</del> method.</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\">drop_duplicates</span><span class=\"mtk15\">(</span><span class=\"mtk8 mtki\">inplace</span><span class=\"mtk12\">=</span><span class=\"mtk5\">True</span><span class=\"mtk15\">)</span></span></span></code></pre>\n<p>Let’s say we want to find out the top 3 stores with the highest July’19 budget.</p>\n<p>We can do so using the <del>nlargest()</del> method, which returns the first <del>n</del> rows ordered by <del>columns</del> in descending order.</p>\n<p>The first argument that we pass to the method is the no. of rows we want to extract. The second argument is the name of the column to order by.</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\">nlargest</span><span class=\"mtk15\">(</span><span class=\"mtk5\">3</span><span class=\"mtk15\">, </span><span class=\"mtk8 mtki\">columns</span><span class=\"mtk12\">=</span><span class=\"mtk16\">&quot;July&#39;19 Budget&quot;</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: 537px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/90ee8466a0ff0c153087bc76737f0091/673a2/nlargest.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: 24.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA2ElEQVQY0y3P0W6DMAyF4b7/23WdRApUBamxAwlkXUFx7GCmIb77XzrnUn6jTF6Fdd/pUEpRVRYWkZyJmYkopZQP6UApMfNFi2iRUso8ze3jYUz1etkYY920CNZUVd/3TdN8Xa/mbrquuxtT1/X37da0zWU/CMswDCICAKMPiO757EopPozvz0dEpmmyAIko58zMCND33Rmr6rIsquocxnkGAGshE/ng10Sq+o4/1oLI/8Zt2wZERDhjZg4hEJH3ox89OOengAAWYFnXnCnO0Vo4DxMNziHiH8FpGRxRs+DyAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"nlargest\"\n        title=\"nlargest\"\n        src=\"/static/90ee8466a0ff0c153087bc76737f0091/673a2/nlargest.png\"\n        srcset=\"/static/90ee8466a0ff0c153087bc76737f0091/56d15/nlargest.png 200w,\n/static/90ee8466a0ff0c153087bc76737f0091/d9f49/nlargest.png 400w,\n/static/90ee8466a0ff0c153087bc76737f0091/673a2/nlargest.png 537w\"\n        sizes=\"(max-width: 537px) 100vw, 537px\"\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<h3 id=\"nsmallest\" style=\"position:relative;\"><a href=\"#nsmallest\" aria-label=\"nsmallest permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>nsmallest()</h3>\n<p>Returns the first <del>n</del> rows ordered by columns in ascending order.</p>\n<p><del>nsmallest()</del> is the complementary method to <del>nlargest()</del>.</p>\n<p>In the following code example, we find out the bottom 3 stores with the lowest July’19 budget.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"3\"><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\">nsmallest</span><span class=\"mtk15\">(</span><span class=\"mtk5\">3</span><span class=\"mtk15\">, </span><span class=\"mtk8 mtki\">columns</span><span class=\"mtk12\">=</span><span class=\"mtk16\">&quot;July&#39;19 Budget&quot;</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: 531px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/80bfc1ca3209aa31d91b516f7d2f0655/b2c14/nSmallest.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: 24.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA4UlEQVQY0y2PXY+CMBAA/f+/7vQFqETqqUDLrh6Rwn506cXLzfskMwd9gc4/ZS9mmYlVtZSym6kqM6uIqhLRtm2qqiJExMQimnM+7GZl35l56Pu6aqqqwucrhOCcu91uZ+cu3aVt26/jsXHN1fuqrl1TH0+nrvOH8sf7/Z7neV2T9x7xeW7bRz8Ic4gxpVVVAab7/S4inxzV/vHo++FfXtO6LImJxjEAgPddiGCWAZGIzQwRxmE0MxHJOY/jEGL8l4loWRbathimGKP/vk4TMBMgprSKMCKGGJn588wcpwgIv8nkGXK04ainAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"nsmallest\"\n        title=\"nsmallest\"\n        src=\"/static/80bfc1ca3209aa31d91b516f7d2f0655/b2c14/nSmallest.png\"\n        srcset=\"/static/80bfc1ca3209aa31d91b516f7d2f0655/56d15/nSmallest.png 200w,\n/static/80bfc1ca3209aa31d91b516f7d2f0655/d9f49/nSmallest.png 400w,\n/static/80bfc1ca3209aa31d91b516f7d2f0655/b2c14/nSmallest.png 531w\"\n        sizes=\"(max-width: 531px) 100vw, 531px\"\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":"Pandas nlargest() & nsmallest() Methods","date":"2021-08-10"}}},"pageContext":{"slug":"/pandas-nlargest-and-nsmallest-methods/","prev":{"fields":{"slug":"/find-values-within-a-range-in-a-pandas-series/"},"frontmatter":{"modules":null}},"next":{"fields":{"slug":"/pandas-set-index-and-reset-index-methods/"},"frontmatter":{"modules":null}}}},
    "staticQueryHashes": ["3159585216"]}