How to Convert Index to Column in Pandas DataFrame?

Python’s Pandas library is one of the most popular libraries for manipulating and analyzing data.  DataFrames are two-dimensional or 2-D tabular data structures with rows and columns in Pandas. Many tools are provided for handling and modifying data in DataFrames, including the ability to convert indexes into columns.

Changing the index from a Pandas DataFrame to a column is a common data analysis task and can be useful when manipulating data. 

This tutorial will provide a comprehensive guide on converting the index to a column in Pandas DataFrame using appropriate examples.

Using df.reset_index() Function in Pandas DataFrame

The “df.reset_index()” function is used to reset the index back to the default value in Python. We can use the “df.reset_index()” method to convert the index of DataFrame to a column. Here is an example of how we can accomplish that in Python:

Example 1: Convert Index to Column

The below code uses the “df.reset_index()” function to convert the index to a column in Pandas DataFrame:

Code: 

import pandas

data = {'Name': ['Joseph','Alex','Lily','Henry'],
        'Age': [24, 25, 30, 24]}

data_frame = pandas.DataFrame(data, columns = ['Name','Age'],
                              index = ['a','b','c','d'])
print(data_frame)
data_frame.reset_index(inplace=True)
print('\n',data_frame)
  • The “pandas” module is imported at the start of the program.
  • The “pd.DataFrame()” function accepts the dictionary data, i.e., column names and index values, as arguments. 
  • The function retrieves a DataFrame that includes the columns and specified index values.
  • The “df.reset_index()” function converts the specified index to a column by resetting the index value to the default index in pandas DataFrame.
  • The “inplace=True” parameter performs the resetting operation on the current dataframe.

Output: 

A specific index has been converted into a column.

Example 2: Rename Index Columns

The below code is used to rename the index column of pandas DataFrame:

Code: 

import pandas

data = {'Name': ['Joseph','Alex','Lily','Henry'],
        'Age': [24, 25, 30, 24]}

data_frame = pandas.DataFrame(data, columns = ['Name','Age'],
                              index = ['a','b','c','d'])
data_frame.reset_index(inplace=True)

data_frame = data_frame.rename(columns = {'index':'New Index'})
print(data_frame)

The “df.rename()” function accepts the new header value as an argument and retrieves the data frame with the new index column name.

Output: 

The specific index/column name has been renamed.

Example 3: Convert MultiIndex to Multiple Columns

The below code is used to convert the multi-index of the dataframe to multiple columns:

Code: 

import pandas
data = {'Name': ['Joseph','Alex','Lily','Henry'],
        'Age': [24, 25, 30, 24]}
index_new = pandas.MultiIndex.from_tuples([('a','a1'),
                                      ('b','b1'),
                                      ('c','c1'),
                                      ('d','d1'),], names=['I1','I2'])
data_frame = pandas.DataFrame(data, columns = ['Name','Age'], index=index_new)
print(data_frame)
data_frame.reset_index(inplace=True)
print('\n',data_frame)
  • A module named “pandas” is imported.
  • The dictionary data is initialized in a variable named “data”.
  • The “MultiIndex.from_tuples()” function takes the multiple index column value as an argument and stores it in a variable named “index_new”.
  • The “pd.DataFrame()” function accepts the dictionary data, column name, and index name as an argument to convert into DataFrame.
  • The “df.reset_index()” function reset the specific multi-index to the default index by converting the multi-index to multiple columns.

Output: 

The specific multi-index has been converted into multiple columns.

Conclusion

In Python, the “df.reset_index()” function converts the single index to columns or multi-index to multiple columns. The “df.rename()” function is used to rename the header value of the index columns. We can also convert the index of multiple indexes to multiple columns using the “df.reset_index()” function and pandas.MultiIndex.from_tuples() function. This guide provided an in-depth guide on how to convert the index to a column in Pandas DataFrame.