Convert Index to Column in Pandas DataFrame

To convert an index to a 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.rename(columns={"index": "new_column_name"}, inplace=True)

Steps to Convert Index to Column in Pandas DataFrame

Step 1: Create a DataFrame

To begin, create a DataFrame with a specific index:

import pandas as pd

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

df = pd.DataFrame(data, 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:

         Product  Price
Item_1  Computer   1200
Item_2   Printer    250
Item_3   Monitor    400
Item_4      Desk    700
Item_5     Phone    350

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

Step 2: Convert the Index to a Column

To convert the index to a column in Pandas DataFrame:

df.reset_index(inplace=True)

The complete Python code:

import pandas as pd

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

df = pd.DataFrame(data, 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 at the left side:

    index   Product  Price
0  Item_1  Computer   1200
1  Item_2   Printer    250
2  Item_3   Monitor    400
3  Item_4      Desk    700
4  Item_5     Phone    350

To rename the “index” header to a customized header called “Items“:

import pandas as pd

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

df = pd.DataFrame(data, index=["Item_1", "Item_2", "Item_3", "Item_4", "Item_5"])

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

print(df)

You’ll now see the new column name:

    Items   Product  Price
0  Item_1  Computer   1200
1  Item_2   Printer    250
2  Item_3   Monitor    400
3  Item_4      Desk    700
4  Item_5     Phone    350

Convert MultiIndex to Multiple Columns in Pandas DataFrame

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, index=new_index)

print(df)

You’ll now see the MultiIndex:

               Product  Price
Items   Type                 
Item_1  I1    Computer   1200
Item_2  I2     Printer    250
Item_3  I3     Monitor    400
Item_4  I4        Desk    700
Item_5  I5       Phone    350

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, index=new_index)

df.reset_index(inplace=True)

print(df)

You’ll now get the two new columns:

    Items  Type   Product  Price
0  Item_1    I1  Computer   1200
1  Item_2    I2   Printer    250
2  Item_3    I3   Monitor    400
3  Item_4    I4      Desk    700
4  Item_5    I5     Phone    350

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

For instance, to 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, index=new_index)

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

print(df)

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

       Type   Product  Price
Items                       
Item_1   I1  Computer   1200
Item_2   I2   Printer    250
Item_3   I3   Monitor    400
Item_4   I4      Desk    700
Item_5   I5     Phone    350

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

Leave a Comment