{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/pandas-set-index-and-reset-index-methods/",
    "result": {"data":{"markdownRemark":{"html":"<div class=\"table-of-contents\">\n<ul>\n<li><a href=\"#set_index\">set_index()</a></li>\n<li><a href=\"#reset_index\">reset_index()</a></li>\n</ul>\n</div>\n<h3 id=\"set_index\" style=\"position:relative;\"><a href=\"#set_index\" aria-label=\"set_index 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>set_index()</h3>\n<p>Let’s read the <del>budget.xlsx</del> 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: 562px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/f13b55a51b8c09e749fed75213bbf7cb/a4f00/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: 68.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAIAAACgpqunAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAB+UlEQVQoz1WSWW7jMBAFc//rxQGyjRNxlSjuFHe1NJAd20l/knwo1GM/NW+qltAaAEgpjdGl1n3fQwgpJaXUEhcxCyGENdZaM0s5HyNjjE/7ZXrv0zQRQl5fXxHGBOOP9/ePz38vp2eEMaP07e395XRinB2XCJ2eT4zSnzAAhBC2beOM+xCEEN8ILSkpOaeU9n23znLOATbYYO0ro8wY+wh772utGGEfFq315/lcajVae+9771IpSlkppdaSYvoeBqX0T3jbIMYFABhjMWVr7XkYWu/e2pjilTxN07oCwNpqI4Qa+4vsrKu1EIKlUoySYUDjJDBCIYTeu9KaUFJLLZehhD7IALAsy7ZtUknrPELoeNqaMebqvCyBc3Y4A2zbNo2T/e1sjCklI4S0seM4fp7PORctpXOutaa1RgjlXHLOuRSE0DzLu/OWUgJYOedhib13zvm6Hi3e2nbHCcB6GUqpNuYPudZKMFZKjSNHCIVlmYXw3rfW/BFmV+FayjAMUqk/XwUAxyY5xxhDGGtjGKU5533fnfOMjw8ywUbfClt7F0LUWhmj1jrv7df56/7PF2fFGb+SSz6qmeXNeV1XYwwA8HH0wXNGKSFhiXIW9w3DmMCV3FeMiX6Qj7CttUopQwjG6HkWF/kxxthaO5ZEiItvySmP4+Sc+w8McBiOKMHA6wAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Budget\"\n        title=\"Budget\"\n        src=\"/static/f13b55a51b8c09e749fed75213bbf7cb/a4f00/budget.png\"\n        srcset=\"/static/f13b55a51b8c09e749fed75213bbf7cb/56d15/budget.png 200w,\n/static/f13b55a51b8c09e749fed75213bbf7cb/d9f49/budget.png 400w,\n/static/f13b55a51b8c09e749fed75213bbf7cb/a4f00/budget.png 562w\"\n        sizes=\"(max-width: 562px) 100vw, 562px\"\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 the <del>Store Code</del> column to serve as the index of our DataFrame. We can achieve this using the <del>set_index()</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\">set_index</span><span class=\"mtk15\">(</span><span class=\"mtk8 mtki\">keys</span><span class=\"mtk12\">=</span><span class=\"mtk16\">&quot;Store Code&quot;</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>\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></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: 532px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/85bea0fb1592fe3b59c01424205c76a2/15ad6/budgetIndex.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: 78.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAIAAACZeshMAAAACXBIWXMAAA7DAAAOwwHHb6hkAAACJUlEQVQoz1WS6ZKrIBBG5/0f8G4To6IssskiIDSYWzFJTXl+UnSf/rr6a9d8Fwvs+3HSWqu1ppNtCzGElJJz3nufUgohbNsWTnLOX4/WHkc7jqOUAlCOoz0eD39irZVSrqsmlM4Yr6sRfFmWhVBCKA0hfj1Ocs637vb79y+EkDHWGGOtM8Y8PfHJq1384L1LaX8Xt9YAyjAMGGNC6N8/fzChfd8TQp9fN3+/3yhhMcYQgjGm625K63fxK+2EkFK65HLvbiHGVpvW+tWX88VY105SivOMnPc/5lKeZs5FCOH731/rbEqJc5FzTikSgqXS+cR7Nw69sfbHfBwHQkhIFWO8d511bts2rV5mWBhbjfmY0zwj69wl8zShhTGMSd/3XIh5mqWQueRSCsFYSgkA5yCJEGKMvWRmjEkh6MK+v28xxVqrUhoqtNYWxtRnin3fKSU/Yz9fSxn6nrElxvAaO8YohCwnGGMh3uYY4zgO62ouZjzPnIvjOAjGWwgAoJSutRYojFKt19oqAKSUxnG8ZC4A04QYWwTnXdcZ+zyVV2YAEFxIIQuUj3m8bLvWSikRUi5c9MNdaU0w1acZACglel1rfWeeJnTJXADmaeKc72kf+t55D1CFEKWUlBKlVK9rKSXnEmIYxn4118wIISkEX/i9u1vrnHNSKqi1tUbwLKT8bDshNF7MOWdKiZRq855gopSepmnV6+vCGHsu7H1hm5/nyTn3H9sTjE57b6G5AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"New Index\"\n        title=\"New Index\"\n        src=\"/static/85bea0fb1592fe3b59c01424205c76a2/15ad6/budgetIndex.png\"\n        srcset=\"/static/85bea0fb1592fe3b59c01424205c76a2/56d15/budgetIndex.png 200w,\n/static/85bea0fb1592fe3b59c01424205c76a2/d9f49/budgetIndex.png 400w,\n/static/85bea0fb1592fe3b59c01424205c76a2/15ad6/budgetIndex.png 532w\"\n        sizes=\"(max-width: 532px) 100vw, 532px\"\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>As we can see above, the <del>Store Code</del> column is no longer a regular column in our DataFrame - it has become the new index.</p>\n<h3 id=\"reset_index\" style=\"position:relative;\"><a href=\"#reset_index\" aria-label=\"reset_index 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>reset_index()</h3>\n<p>There is a complementary method called <del>reset_index()</del>, which moves the current index back into a column position and creates a brand new numeric pandas index.</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\">reset_index</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: 554px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/79a14ef60b9ba6f87f9ace9361b152ff/b4bb7/budgetResetIndex.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: 71.00000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAIAAACgpqunAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAB/klEQVQoz22S2XLjIBRE8/+fF1cmFaditIBYJZaL2ITQjOxMuWbp90NzGl6SVmlRtZRaq3MOAGqt7ThCCCklAIghGGONNiEEAGetcdYCQM7l5bin1soYu6Hb+/s7pZRR+nn9RDf04+1tGPobQq+Xy8fHteu62+32eb1eLq/zrL/hfd+ttbVWjIk1FuOxH0ZjDOcsxngch1SSc9721lpLKeFx9H59ws5BznnohxijkOILda21ZVbe+23bhBSci3KP977rOg/+CQPAtm3jiGPKUkrUD8dxGL2EEPZ9l0pKqeo9IYRhGP5ottamlPq+19oQQvphkFJOBK/rWmuVUlLGSik55xDiOIzOwW+47R58rVVIsSwadR0hU4xRKflwXvTCOd/3VmstpTBGvffPtbXWKZ1HWuv6Yej6vtZ9ntVDRylFpqmUklJ6XNs59w231kIItW4EkxhjSpFMtLVmjI4xttaUUlzIR3POeTzX/rv5dLbWcTZhjEspSgoAKKVorTmj+Z51XTuE4N+1pZTaGIS6iTEpFKVTyvk4jhM+m/fTOWdC8LM5pTTPc84ZYwwARuuu67dtm5W8/8QsBJdC3YvTuq4Ioadzztk6d35SSsE5TEaM8QnPKjzWnmdKWWv/c845L8uScyJkAvCcU8Y5pezxzqUUIQRj7OEM4Pu+/wX/BI1cGMoqp301AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Index Reset\"\n        title=\"Index Reset\"\n        src=\"/static/79a14ef60b9ba6f87f9ace9361b152ff/b4bb7/budgetResetIndex.png\"\n        srcset=\"/static/79a14ef60b9ba6f87f9ace9361b152ff/56d15/budgetResetIndex.png 200w,\n/static/79a14ef60b9ba6f87f9ace9361b152ff/d9f49/budgetResetIndex.png 400w,\n/static/79a14ef60b9ba6f87f9ace9361b152ff/b4bb7/budgetResetIndex.png 554w\"\n        sizes=\"(max-width: 554px) 100vw, 554px\"\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 set_index() & reset_index() Methods","date":"2021-08-10"}}},"pageContext":{"slug":"/pandas-set-index-and-reset-index-methods/","prev":{"fields":{"slug":"/pandas-nlargest-and-nsmallest-methods/"},"frontmatter":{"modules":null}},"next":{"fields":{"slug":"/using-the-in-keyword-to-check-whether-a-value-exists-in-a-pandas-series/"},"frontmatter":{"modules":null}}}},
    "staticQueryHashes": ["3159585216"]}