Skip to content
Related Articles

Related Articles

How to convert index in a column of the Pandas dataframe?
  • Last Updated : 01 Aug, 2020

Each row in a dataframe (i.e level=0) has an index value i.e value from 0 to n-1 index location and there are many ways to convert these index values into a column in a pandas dataframe. First, let’s create a Pandas dataframe. Here, we will create a Pandas dataframe regarding student’s marks in a particular subject with columns roll number, name, score, grade, and subject.

Example:

Python3




# importing the pandas library as pd 
import pandas as pd     
  
  
# Creating the dataframe Ab 
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250'
                                   '9917102203', '9917102204',
                                   '9917102231'], 
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA'
                            'PRAKHAR','ASHISH'], 
                   'Score': [99, 98, 50, 45,97], 
                   'Grade': ['A+', 'A+', 'C+', 'C','A'], 
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']}) 
    
# Printing the dataframe 
AB

Output:

DATA FRAME CREATED

Method 1: Creating a new Index column



Here we will learn to create a new column in the existing dataframe as an index and add index value of each row(level=0) to that column.

Python3




# importing the pandas library as pd 
import pandas as pd     
  
  
# Creating the dataframe Ab 
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250'
                                   '9917102203', '9917102204',
                                   '9917102231'], 
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'], 
                   'Score': [99, 98, 50, 45,97], 
                   'Grade': ['A+', 'A+', 'C+', 'C','A'], 
                   'Subject': ['Operating Systems', 'Operating Systems'
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']}) 
    
      
# Adding a new index column to existing
# data frame and passing index value   
AB['index'] = AB.index
  
# Printing the dataframe 
AB

Output:

Here, we added a new column “index” to “AB” dataframe using the index value of each row in the dataframe as an argument value and converted index to the column.

Method 2: Using reset_index() method and to_string() method

Here we will sue reset_index() method to convert the index to a column along with inplace argument to reflect the change continuously and we will use the to_string() method for hiding the index value shown by default whenever a dataframe is printed.

Python3




#CREATING A DATAFRAME FOR STUDENTS PORTFOLIO
  
# importing the pandas library as pd 
import pandas as pd     
# Creating the dataframe Ab 
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250', '9917102203', '9917102204','9917102231'], 
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA', 'PRAKHAR','ASHISH'], 
                   'Score': [99, 98, 50, 45,97], 
                   'Grade': ['A+', 'A+', 'C+', 'C','A'], 
                   'Subject': ['Operating Systems', 'Operating Systems', 'Operating Systems', 'Operating Systems','Operating Systems']}) 
    
      
# importing the pandas library as pd 
import pandas as pd     
  
  
# Creating the dataframe Ab 
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250'
                                   '9917102203', '9917102204',
                                   '9917102231'], 
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'], 
                   'Score': [99, 98, 50, 45,97], 
                   'Grade': ['A+', 'A+', 'C+', 'C','A'], 
                   'Subject': ['Operating Systems', 'Operating Systems'
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']}) 
    
      
# USING RESET_INDEX METHOD
# Adding a new index column to AB dataframe
AB.reset_index(level=0, inplace=True
  
# HIDING THE DEFAULT INDEX VALUES AND
# PRINTING DATAFRAME
print( AB.to_string(index=False))

Output:



USING RESET_INDEX METHOD

Here we applied reset_index method to given data frame and make the default index value (equal to 0) by passing it as an arguement to to_string method.

Method 3: Using multi_index

Here We will learn to create columns from a dataframe having multi-index.

Example 1: For multi-index to column

Python3




# importing the pandas library as pd 
import pandas as pd     
  
# ADDING MULTI INDEX TO DATA FRAME
new_index = pd.MultiIndex.from_tuples([('E4','ECE'),
                                       ('E5','ECE'),
                                       ('E6','ECE'),
                                       ('E7','ECE'),
                                       ('E8','ECE')],
                                       names=['BATCH','BRANCH'])
# Creating the dataframe AB
data =({'Roll Number': ['9917102206', '9917102250'
                        '9917102203', '9917102204',
                        '9917102231'], 
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'], 
                   'Score': [99, 98, 50, 45,97], 
                   'Grade': ['A+', 'A+', 'C+', 'C','A'], 
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']}) 
    
# COMBING DATA FRAME AND MULTI INDEX 
# VALUES AND FORMING DATA FRAME
AB = pd.DataFrame(data, columns = ['Roll Number','Name','Score','Grade','Subject'], 
                  index=new_index)
  
# MAKING MULTI INDEX NOW A PART OF COLUMN 
# OF DATAFRAME
AB.reset_index(inplace=True
  
AB

Output:

Example 2: For making specific column from multi-index as a column of dataframe

Python3




# importing the pandas library as pd 
import pandas as pd     
  
# ADDING MULTI INDEX TO DATA FRAME
new_index = pd.MultiIndex.from_tuples([('E4','ECE'),
                                       ('E5','ECE'),
                                       ('E6','ECE'),
                                       ('E7','ECE'),
                                       ('E8','ECE')],
                                       names=['BATCH','BRANCH'])
# Creating the dataframe AB
data =({'Roll Number': ['9917102206', '9917102250'
                        '9917102203', '9917102204',
                        '9917102231'], 
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'], 
                   'Score': [99, 98, 50, 45,97], 
                   'Grade': ['A+', 'A+', 'C+', 'C','A'], 
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']}) 
    
# COMBING DATA FRAME AND MULTI INDEX 
# VALUES AND FORMING DATA FRAME
AB = pd.DataFrame(data, columns = ['Roll Number','Name','Score','Grade','Subject'],
                  index=new_index)
  
# MAKING SPECIFIC COLUMN OF MULTI INDEX 
# NOW A PART OF COLUMN OF DATAFRAME
AB.reset_index(inplace=True,level=['BATCH']) 
  
# BATCH INDEX IS NOW A COLUMN OF DATAFRAME
AB

Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :