In this article, we will discuss how to convert indexes of a dataframe or a multi-index dataframe into its columns.

Pandas Dataframe class provides a function to reset the indexes of the dataframe i.e.

Dataframe.reset_index()

It resets the indexes of the dataframe and returns a dataframe with a new index.
Important arguments are as follows,

  • level : int, str or list of str
    • By default, reset_index() resets all the indexes of the dataframe. In 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.
  • drop: bool, by default False
    • If False, then converts the index to a column else removes the index from the dataframe.
  • inplace: bool, default False
    • If True, then modifies the dataframe in place.

Returns:

  • If inplace argument is True then returns None and modifies the existing dataframe. Whereas, if inplace is False, then returns a copy of existing dataframe object with modifications, i.e. doesn’t alter calling dataframe object.

We will use this function to convert the indexes of a dataframe to columns.

First of all, create a dataframe and set its index, i.e.

Contents of the created dataframe object empDfObj are,

Now let’s experiment with this dataframe,

Convert index of a Dataframe into a column of dataframe

To convert the index ‘ID‘ of the dataframe empDfObj into a column, call the reset_index() function on that dataframe,

Output:

As we didn’t provided inplace argument, so by default it returned a modified copy of the dataframe. In which index ‘ID’ gets converted into the column ‘ID’ in the dataframe, and a new default index gets assigned to it. Now, if you want to make changes in the existing dataframe then either assign it back to empDFObj like,

or pass implace argument as True, i.e.

Output:

It modified the dataframe object empDfObj in place.

Let’s set the column ‘ID’ as index of the dataframe for more examples,

Contents of the Dataframe object empDfObj are now,

Remove index  of dataframe instead of converting into column

In previous example, we converted the dataframe index in to the column but what if we just want to remove the index of the dataframe instead of moving it as column. We can do that by passing drop argument as True in the reset_index() function,

Output

It removed the index of the dataframe and assigned a default index to the dataframe.

Resetting indexes of a Multi-Index Dataframe

Let’s convert the dataframe object empDfObj  into a multi-index dataframe with two indexes i.e. ID & Name,

Contents of the multi-index dataframe empDfObj are,

Convert all the indexes of Multi-index Dataframe to the columns of Dataframe

To convert all the indexes of a multi-index dataframe to columns with same, just call the reset_index() on the dataframe object i.e.

Output:

It converted the indexes ‘ID’ & ‘Name’ to the columns with same name in the dataframe.

What if we want to convert the only one index of the multi-index dataframe in to the column. We can do that by passing the index name in the level argument i.e.

Output

It converted the index ‘ID’ into the column ‘ID’ in the dataframe. Whereas, index ‘Name’ remains as it is.
Let’s look at an another example,

Output

It converted the index ‘Name’ into the column ‘Name’ in the dataframe. Whereas, index ‘ID’ remains as it is.

We can pass the multiple column names in the level argument as a list i.e.

Output:

It converted both the indexes ‘ID’ & ‘Name’ into the columns of the dataframe.

The complete example is as follows,

Output:

Python Resources

C++11 / C++14 Resources

Design Patterns Resources

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,