{"id":4225,"date":"2021-04-27T19:38:14","date_gmt":"2021-04-27T14:08:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=4225"},"modified":"2021-11-22T18:43:02","modified_gmt":"2021-11-22T13:13:02","slug":"pandas-convert-data-frame-index-into-column-using-dataframe-reset_index-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-convert-data-frame-index-into-column-using-dataframe-reset_index-in-python\/","title":{"rendered":"Pandas : Convert Data frame index into column using dataframe.reset_index() in python"},"content":{"rendered":"
In this article, we will be exploring ways to convert indexes of a data frame or a multi-index data frame into its a column.<\/p>\n
There is a function provided in the Pandas Data frame class to reset the indexes of the data frame.<\/p>\n
DataFrame.<\/span>reset_index<\/span>(<\/span>self, level=<\/span>None<\/span>, drop=<\/span>False<\/span>, inplace=<\/span>False<\/span>, col_level=<\/span>0<\/span>, col_fill=<\/span>''<\/span>)<\/span><\/pre>\nIt returns a data frame with the new index after resetting the indexes of the data frame.<\/p>\n
\n- level<\/strong>: By default, reset_index() resets all the indexes of the data frame. In the case of a multi-index dataframe, if we want to reset some specific indexes, then we can specify it as int, str, or list of str, i.e., index names.<\/li>\n
- Drop<\/strong>:\u00a0If False, then converts the index to a column else removes the index from the dataframe.<\/li>\n
- Inplace<\/strong>: If true, it modifies the data frame in place.<\/li>\n<\/ul>\n
Let’s use this function to convert the indexes of dataframe to columns.<\/p>\n
The first and the foremost thing we will do is to create a dataframe and initialize it’s index.<\/p>\n
Code:<\/pre>\nempoyees = <\/span>[(<\/span>11<\/span>, <\/span>‘jack’<\/span>, <\/span>34<\/span>, <\/span>‘Sydney’<\/span>, <\/span>70000<\/span>)<\/span> ,<\/span><\/div>\n(<\/span>12<\/span>, <\/span>‘Riti’<\/span>, <\/span>31<\/span>, <\/span>‘Delhi’<\/span> , <\/span>77000<\/span>)<\/span> ,<\/span><\/div>\n(<\/span>13<\/span>, <\/span>‘Aadi’<\/span>, <\/span>16<\/span>, <\/span>‘Mumbai’<\/span>, <\/span>81000<\/span>)<\/span> ,<\/span><\/div>\n(<\/span>14<\/span>, <\/span>‘Mohit’<\/span>, <\/span>31<\/span>,<\/span>‘Delhi’<\/span> , <\/span>90000<\/span>)<\/span> ,<\/span><\/div>\n(<\/span>15<\/span>, <\/span>‘Veena’<\/span>, <\/span>12<\/span>, <\/span>‘Delhi’<\/span> , <\/span>91000<\/span>)<\/span> ,<\/span><\/div>\n(<\/span>16<\/span>, <\/span>‘Shaunak’<\/span>, <\/span>35<\/span>, <\/span>‘Mumbai’<\/span>, <\/span>75000<\/span> )<\/span>,<\/span><\/div>\n(<\/span>17<\/span>, <\/span>‘Shaun’<\/span>, <\/span>35<\/span>, <\/span>‘Colombo’<\/span>, <\/span>63000<\/span>)<\/span>]<\/span><\/div>\n# Create a DataFrame object<\/span><\/div>\nempDfObj = pd.<\/span>DataFrame<\/span>(<\/span>empoyees, columns=<\/span>[<\/span>‘ID’<\/span> , <\/span>‘Name’<\/span>, <\/span>‘Age’<\/span>, <\/span>‘City’<\/span>, <\/span>‘Salary’<\/span>])<\/span><\/div>\n# Set ‘ID’ as the index of the dataframe<\/span><\/div>\nempDfObj.<\/span>set_index<\/span>(<\/span>‘ID’<\/span>, inplace=<\/span>True<\/span>)<\/span><\/div>\nprint<\/span>(<\/span>empDfObj<\/span>)<\/span><\/div>\n<\/p>\n
Now, we will try different things with this dataframe.<\/p>\n
Convert index of a Dataframe into a column of dataframe<\/h3>\n
To convert the index \u2018ID<\/em>\u2018 of the dataframe\u00a0empDfObj<\/em>\u00a0into a column, call the reset_index() function on that dataframe,<\/p>\nCode:<\/pre>\nmodified = empDfObj.<\/span>reset_index<\/span>()<\/span><\/div>\nprint<\/span>(<\/span>“Modified Dataframe : “<\/span>)<\/span><\/div>\nprint<\/span>(<\/span>modified<\/span>)<\/span><\/div>\n\n<\/p>\n
Since we haven’t provided the inplace<\/em> argument, so by default it returned the modified copy of a dataframe.<\/p>\nIn which the indexID is converted into a column named ‘ID’ and automatically the new index is assigned to it.<\/p>\n
Now, we will pass the\u00a0inplace\u00a0<\/em>argument as True to proceed with the process.<\/p>\n\nCode:<\/span>\r\nempDfObj.<\/span>reset_index<\/span>(<\/span>inplace=<\/span>True<\/span>)\r\n<\/span>print(<\/span>empDfObj<\/span>)<\/span><\/pre>\n<\/p>\n
Now, we will set the column’ID’ as the index of the dataframe.<\/p>\n
Code:\r\nempDfObj.<\/span>set_index<\/span>(<\/span>'ID'<\/span>, inplace=<\/span>True<\/span>)<\/span><\/pre>\nRemove index\u00a0 of dataframe instead of converting into column<\/h3>\n
Previously, what we have done is convert the index of the dataframe into the column of the dataframe but now we want to just remove it. We can do that by passing\u00a0drop<\/em>\u00a0argument as\u00a0True<\/em>\u00a0in the reset_index() function,<\/p>\n\nCode:<\/span>\r\nmodified = empDfObj.<\/span>reset_index<\/span>(<\/span>drop=<\/span>True<\/span>)\r\n<\/span>print(<\/span>\"Modified Dataframe : \"<\/span>)\r\n<\/span>print(<\/span>modified<\/span>)<\/span><\/pre>\n<\/p>\n
We can see that it removed the dataframe index.<\/p>\n
Resetting indexes of a Multi-Index Dataframe<\/h3>\n
Let\u2019s convert the dataframe object\u00a0empDfObj<\/em>\u00a0 into a multi-index dataframe with two indexes i.e. ID & Name.<\/p>\nCode:<\/pre>\nempDfObj = pd.<\/span>DataFrame<\/span>(<\/span>empoyees, columns=<\/span>[<\/span>'ID'<\/span>, <\/span>'Name'<\/span>, <\/span>'Age'<\/span>, <\/span>'City'<\/span>, <\/span>'Salary'<\/span>])<\/span>\r\n# set multiple columns as the index of the the dataframe to<\/span>\r\n# make it multi-index dataframe.<\/span>\r\nempDfObj.<\/span>set_index<\/span>([<\/span>'ID'<\/span>, <\/span>'Name'<\/span>]<\/span>, inplace=<\/span>True<\/span>)<\/span>\r\nprint<\/span>(<\/span>empDfObj<\/span>)<\/span><\/pre>\n\n<\/p>\n
Convert all the indexes of Multi-index Dataframe to the columns of Dataframe<\/h3>\n
In the previous module, we have made a dataframe with the multi-index but now here we will convert the indexes of multi-index dataframe to the columns of the dataframe.<\/p>\n
To do this, all we have to do is just call the reset_index() on the dataframe object.<\/p>\n
Code:<\/pre>\nmodified = empDfObj.<\/span>reset_index<\/span>()<\/span>\r\nprint<\/span>(<\/span>modified<\/span>)<\/span>\r\n\r\n