Turn Index to Column in a Pandas Dataframe

Pandas DataFrame Convert Index Into Column

Hi there! In this Python tutorial, we are going to discuss how we can convert a DataFrame index to column. We will also see how to convert the multiple levels of indexes of a multi-index DataFrame into its multiple columns. So let’s get started.


What are indexes in the pandas DataFrame?

Pandas is a robust Python library that is widely used for data analysis. It provides us a data structure called DataFrame that stores the data in the form of rows and columns where each row has a unique index value. A pandas DataFrame object can have more than one level of the index in that case it is called MultiIndex DataFrame.

Whenever we create a panda DataFrame object, By default an index value from zero to number of rows – 1 is assigned to each row of the DataFrame in sequential order. Although we can also set the index value for each row of the pandas DataFrame object manually using the DataFrame.set_index() function in pandas.

We can convert the one or more levels of indexes of a pandas DataFrame object into its columns using the following two methods. To demonstrate the process of turning the DataFrame index into a column, let’s first create a pandas DataFrame object.

Also read: Pandas DataFrame Indexing: Set the Index of a Pandas Dataframe

Methods to Convert Index to Column of a Pandas Dataframe

# Import pandas Python module
import pandas as pd

# Create a pandas DataFrame object
df = pd.DataFrame({'Dept': ['ECE', 'ICE', 'IT', 'CSE', 'CHE'],
                    'GPA': [8.15, 9.03, 7.85, 8.55, 9.45],
                    'Name': ['Kirti', 'Sarthak', 'Anubhav', 'Ranjan', 'Kartik'],
                    'RegNo': [111, 112, 113, 114, 115]})

# Set 'RegNo' as index of the pandas DataFrame
df.set_index('RegNo', inplace=True)                    

# Print the created pandas DataFrame object
print('Sample pandas DataFrame:\n')
print(df)

Output:

Sample pandas DataFrame:

      Dept   GPA     Name
RegNo                    
111    ECE  8.15    Kirti
112    ICE  9.03  Sarthak
113     IT  7.85  Anubhav
114    CSE  8.55   Ranjan
115    CHE  9.45   Kartik

Method 1: Create a new DataFrame column and pass the index

This is the simplest method to convert the DataFrame index to column. In this method, we simply create a new column in the DataFrame and pass the index to it using the DataFrame.index method of pandas DataFrame class. Let’s see the Python code to implement this method.

# Method 1

# Convert the index of the sample DataFrame into column
# Using the new column method
df['Roll'] = df.index                    

# Print the modified pandas DataFrame
print('Modified pandas DataFrame:\n')
print(df)

Output:

Modified pandas DataFrame:

      Dept   GPA     Name  Roll
RegNo                          
111    ECE  8.15    Kirti   111
112    ICE  9.03  Sarthak   112
113     IT  7.85  Anubhav   113
114    CSE  8.55   Ranjan   114
115    CHE  9.45   Kartik   115

Method 2: Using the DataFrame.reset_index() function in pandas

This is the widely used method to turn one or more levels of the DataFrame index into one or more columns. In this method, we will use the DataFrame.reset_index() function of pandas DataFrame class. Let’s write the Python code to implement this method.

# Method 2

# Convert the index of the sample DataFrame into column
# Using the DataFrame.reset_index() function
df.reset_index(inplace=True)                    

# Print the modified pandas DataFrame
print('Modified pandas DataFrame:\n')
print(df)

Output:

Modified pandas DataFrame:

   RegNo Dept   GPA     Name
0    111  ECE  8.15    Kirti
1    112  ICE  9.03  Sarthak
2    113   IT  7.85  Anubhav
3    114  CSE  8.55   Ranjan
4    115  CHE  9.45   Kartik

Convert one or more levels of a MultiIndex DataFrame into columns

Let us first convert the above sample DataFrame into a MultiIndex DataFrame by setting the RegNo and Name as the multiple levels of indexes of the sample DataFrame using the DataFrame.set_index() function.

# Convert the sample DataFrame into MultiIndex DataFrame
# By setting the 'RegNo' and 'Name' as Multi-level index
df.set_index(['RegNo', 'Name'], inplace=True)                    

# Print the modified pandas DataFrame
print('Modified Sample pandas DataFrame:\n')
print(df)

Output:

Modified Sample pandas DataFrame:

              Dept   GPA
RegNo Name              
111   Kirti    ECE  8.15
112   Sarthak  ICE  9.03
113   Anubhav   IT  7.85
114   Ranjan   CSE  8.55
115   Kartik   CHE  9.45

Now let’s write the Python code to convert only one of the index levels of the sample MultiIndex DataFrame into a column using the DataFrame.reset_index() function.

# Convert one level of the MultiIndex DataFrame into column
# Using the DataFrame.reset_index() function
df.reset_index(level='Name', inplace=True)                  

# Print the modified pandas DataFrame
print('Modified pandas DataFrame:\n')
print(df)

Output:

Modified pandas DataFrame:

          Name Dept   GPA
RegNo                    
111      Kirti  ECE  8.15
112    Sarthak  ICE  9.03
113    Anubhav   IT  7.85
114     Ranjan  CSE  8.55
115     Kartik  CHE  9.45

Summing-up

In this tutorial, we have learned how to convert the index of a pandas DataFrame into its column. And we have also learned to convert one or more levels of the index of a MultiIndex DataFrame into its columns. Hope you have understood the things discussed above and are ready to experiment with your own pandas DataFrame. Thanks for reading! Stay tuned with us for more amazing learning content related to Python programming.