How to Convert Index to Column in Pandas DataFrame

You may use the following approach to convert index to column in Pandas DataFrame (with an “index” header):

df.reset_index(inplace=True)

And if you want to rename the “index” header to a customized header, then use:

df.reset_index(inplace=True)
df = df.rename(columns = {'index':'new column name'})

Later, you’ll also see how to convert MultiIndex to multiple columns.

Steps to Convert Index to Column in Pandas DataFrame

Step 1: Create the DataFrame

Let’s create a simple DataFrame for a specific index:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone'],
          'Price': [1200,250,400,700,350],
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index = ['Item_1','Item_2','Item_3','Item_4','Item_5'])

print(df)

Run the code in Python, and you’ll see the index with the 5 items:

DataFrame Example

The ultimate goal is to convert the above index into a column.

Step 2: Convert the Index to Column

You may now use this template to convert the index to column in Pandas DataFrame:

df.reset_index(inplace=True)

So the complete Python code would look like this:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone'],
          'Price': [1200,250,400,700,350],
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index = ['Item_1','Item_2','Item_3','Item_4','Item_5'])

df.reset_index(inplace=True)

print(df)

As you can see, the previous index became a column with an “index” header. Also, a new sequential index was added to the left:

How to Convert Index to Column in Pandas DataFrame

But what if you want to rename the “index” header to a customized header?

In that case, you may apply this template:

df.reset_index(inplace=True)
df = df.rename(columns = {'index':'new column name'})

For example, let’s rename the “index” header to “Items” as follows:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone'],
          'Price': [1200,250,400,700,350],
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index = ['Item_1','Item_2','Item_3','Item_4','Item_5'])

df.reset_index(inplace=True)
df = df.rename(columns = {'index':'Items'})

print(df)

You’ll now see the new column name:

Rename column

Convert MultiIndex to Multiple Columns in Pandas DataFrame

So far you have seen how to convert a single index to a column.

Alternatively, you may have a DataFrame with MultiIndex.

Here is an example of a DataFrame with MultiIndex:

import pandas as pd

new_index = pd.MultiIndex.from_tuples([('Item_1','I1'),
                                       ('Item_2','I2'),
                                       ('Item_3','I3'),
                                       ('Item_4','I4'),
                                       ('Item_5','I5')],
                                       names=['Items','Type'])

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone'],
          'Price': [1200,250,400,700,350],
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index=new_index)

print(df)

You’ll now see the MultiIndex:

How to Convert Index to Column in Pandas DataFrame

You can then convert the MultiIndex into multiple columns using this code:

import pandas as pd

new_index = pd.MultiIndex.from_tuples([('Item_1','I1'),
                                       ('Item_2','I2'),
                                       ('Item_3','I3'),
                                       ('Item_4','I4'),
                                       ('Item_5','I5')],
                                       names=['Items','Type'])

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone'],
          'Price': [1200,250,400,700,350],
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index=new_index)

df.reset_index(inplace=True)

print(df)

You’ll now get the two new columns:

Convert Index to Column in Pandas DataFrame

What if you want to select a specific index from the MultiIndex to become a column?

For instance, let’s select the “Type” index to become the new column:

import pandas as pd

new_index = pd.MultiIndex.from_tuples([('Item_1','I1'),
                                       ('Item_2','I2'),
                                       ('Item_3','I3'),
                                       ('Item_4','I4'),
                                       ('Item_5','I5')],
                                       names=['Items','Type'])

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone'],
          'Price': [1200,250,400,700,350],
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index=new_index)

df.reset_index(inplace=True, level = ['Type'])

print(df)

As you may see, the “Type” index is now a new column (while the “Items” index remains an index):

How to Convert Index to Column in Pandas DataFrame

You can check the Pandas Documentation for further information about df.reset_index.