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 a DataFrame
Let’s create a simple 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, 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:
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 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:
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
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:
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
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:
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, columns = ['Product','Price'], 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, 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):
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.