{"id":462,"date":"2023-11-16T11:52:35","date_gmt":"2023-11-16T11:52:35","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=462"},"modified":"2023-11-22T09:16:38","modified_gmt":"2023-11-22T09:16:38","slug":"pandas-reset-index","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/pandas-reset-index\/","title":{"rendered":"Reset Index in Pandas DataFrames (with Examples)"},"content":{"rendered":"\n<p>If you regularly work with data in Python using the powerful Pandas library, you may often find yourself needing to manipulate and filter DataFrames. After performing these operations, you might end up with a smaller DataFrame that still retains the row index of the original one. This can lead to non-continuous or undesired indexes. Pandas provides a handy function called reset_index(), which we learn about in this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the reset_index() Function in Pandas?<\/strong><\/h2>\n\n\n\n<p><strong>The reset_index() method is a powerful tool in Pandas that allows you to reset the index of a DataFrame back to the default integer index (0, 1, 2, &#8230;). <\/strong>By default, this method keeps the original indexes in a column named &#8220;index,&#8221; but you can choose to remove them using the drop parameter.<\/p>\n\n\n\n<p>If you want to remove the column, it can be done using the <a href=\"https:\/\/favtutor.com\/articles\/pandas-drop-column\/\">drop() method<\/a>.<\/p>\n\n\n\n<p>Here&#8217;s a basic syntax for the reset_index() function:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>dataframe.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')<\/code><\/pre>\n\n\n\n<p>Here\u2019s a breakdown of the syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>level: <\/strong>Specifies the levels to reset. By default, it resets all levels.<br><\/li>\n\n\n\n<li><strong>drop<\/strong>: Specifies whether to drop the old index column. The default value is False, which means the old index is kept as a column in the DataFrame.<br><\/li>\n\n\n\n<li><strong>inplace<\/strong>: Specifies whether to modify the DataFrame in place or return a new DataFrame. The default value is False, which returns a new DataFrame with the reset index.<br><\/li>\n\n\n\n<li><strong>col_level<\/strong>: For DataFrames with multi-level columns, determine which level the labels are inserted into. The default value is 0, which inserts the labels into the first level.<br><\/li>\n\n\n\n<li><strong>col_fill<\/strong>: For DataFrames with multi-level columns, determine how the other levels are named. The default value is an empty string (&#8221;), but you can specify a custom name.<br><\/li>\n<\/ul>\n\n\n\n<p>Now that we have an understanding of the reset_index() method, let&#8217;s explore different scenarios and examples to reset the index in a Pandas DataFrame.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating a Custom Index Without Removing the Default Index<\/strong><\/h3>\n\n\n\n<p>In some cases, you may want to create a custom index for your DataFrame without removing the default index. This can be achieved by passing a list or array of values to the index parameter when creating the DataFrame.<\/p>\n\n\n\n<p>Let&#8217;s consider the following 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;Sally&quot;, &quot;Mary&quot;, &quot;John&quot;],\n    &quot;Age&quot;: [50, 40, 30],\n    &quot;Qualified&quot;: [True, False, False]\n}\n\n# Create the custom index\nindex = [&quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;]\n\ndf = pd.DataFrame(data, index=index)\n# Display DataFrame\nprint('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>DataFrame:\n     Name  Age  Qualified\nX  Sally   50       True\nY   Mary   40      False\nZ   John   30      False\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating a Custom Index and Removing the Default Index<\/strong><\/h3>\n\n\n\n<p>If you want to create a custom index for your DataFrame and remove the default index, you can use the reset_index() method with the drop parameter set to True.<\/p>\n\n\n\n<p><strong>The reset_index() method removes the old index and replaces it with a new default integer index. The drop=True parameter ensures that the old index is not added as a column in the DataFrame.<\/strong><\/p>\n\n\n\n<p>Continuing from the previous example, let&#8217;s reset the index and remove the default index:<\/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;Sally&quot;, &quot;Mary&quot;, &quot;John&quot;],\n    &quot;Age&quot;: [50, 40, 30],\n    &quot;Qualified&quot;: [True, False, False]\n}\n\n# Create custom index\nindex = [&quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;]\n\ndf = pd.DataFrame(data, index=index)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# To reset the index to the default integer index\ndf.reset_index(drop=True, inplace=True)\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><br><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  Qualified\nX  Sally   50       True\nY   Mary   40      False\nZ   John   30      False\n\n\nModified DataFrame:\n     Name  Age  Qualified\n0  Sally   50       True\n1   Mary   40      False\n2   John   30      False\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Resetting the Index to the Default Integer Index<\/strong><\/h3>\n\n\n\n<p>When you want to reset the index of a DataFrame to the default integer index, you can simply use the reset_index() method without any parameters. <strong>This will remove the existing index and replace it with a new sequential index starting from 0.<\/strong><\/p>\n\n\n\n<p>Let&#8217;s consider the following 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;Sally&quot;, &quot;Mary&quot;, &quot;John&quot;],\n    &quot;Age&quot;: [50, 40, 30],\n    &quot;Qualified&quot;: [True, False, False]\n}\n\nindex = [&quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;]\n\ndf = pd.DataFrame(data, index=index)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# To reset the index to the default integer index\nnewdf = df.reset_index()\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', newdf)<\/pre><\/div>\n\n\n\n<p><br><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  Qualified\nX  Sally   50       True\nY   Mary   40      False\nZ   John   30      False\n\nModified DataFrame:\n   index   Name  Age  Qualified\n0     X  Sally   50       True\n1     Y   Mary   40      False\n2     Z   John   30      False\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Making a Column of the DataFrame as the Index<\/strong><\/h3>\n\n\n\n<p>In some cases, you may want to reset a custom index back to the default integer index and make the default index the new index of the DataFrame. This can be achieved by using the reset_index() method with the drop parameter set to True.<\/p>\n\n\n\n<p>Let&#8217;s consider the following 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;Sally&quot;, &quot;Mary&quot;, &quot;John&quot;],\n    &quot;Age&quot;: [50, 40, 30],\n    &quot;Qualified&quot;: [True, False, False]\n}\n\n# Create custom index\nindex = [&quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;]\n\ndf = pd.DataFrame(data, index=index)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# To reset the index as the column 'Age'\ndf.set_index('Age', inplace=True)\n\n# Display the modified DataFrame\nprint('Modified 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  Qualified\nX  Sally   50       True\nY   Mary   40      False\nZ   John   30      False\n\nModified DataFrame:\nAge   Name  Qualified\n                \n50   Sally       True\n40    Mary      False\n30    John      False\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we explored various methods to reset the index in a Pandas DataFrame.\u00a0We learned how to reset the index to the default integer index, create a custom index, remove the default index, and make a column the new index.\u00a0 By mastering this method, you can efficiently manipulate and transform your data in Pandas, ensuring your DataFrames are organized and structured according to your requirements. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand the reset_index() function in Pandas. Also, learn various methods to use it along with Examples and Codes.<\/p>\n","protected":false},"author":10,"featured_media":464,"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":[36,37,32],"class_list":["post-462","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-data-science","tag-pandas","tag-python"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/462","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=462"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/462\/revisions"}],"predecessor-version":[{"id":527,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/462\/revisions\/527"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/464"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}