Python Pandas : Replace or change Column & Row index names in DataFrame

In this article we will discuss how to change column names or Row Index names in DataFrame object.

First of all, create a dataframe object of students records i.e.

students = [ ('jack', 34, 'Sydeny') ,
             ('Riti', 30, 'Delhi' ) ,
             ('Aadi', 16, 'New York') ]

# Create a DataFrame object
dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City'], index=['a', 'b', 'c']) 

Contents of DataFrame objects are as follows,

   Name  Age      City
a  jack   34    Sydeny
b  Riti   30     Delhi
c  Aadi   16  New York

Change Column Names in DataFrame

DataFrame object has an Attribute columns that is basically an Index object and contains column Labels of Dataframe. We can get the ndarray of column names from this Index object i.e.

# Get ndArray of all column names 
columnsNamesArr = dfObj.columns.values

Any modification in this ndArray (df.column.values) will modify the actual DataFrame. For example let’s change the name of column at index 0 i.e.

# Get ndArray of all column names 
columnsNamesArr = dfObj.columns.values

# Modify a Column Name
columnsNamesArr[0] = 'Test'

This change will be reflected in linked DataFrame object too. Now contents of DataFrame object is,

   Test  Age      City
a  jack   34    Sydeny
b  Riti   30     Delhi
c  Aadi   16  New York

But we convert it to list before modifying then changes will not be reflected in original DataFrame object.

For example create a list of Column Names of DataFrame i.e.

# get a copy list of all the column names 
columnsNames = list(dfObj.columns.values)

or

columnsNames = list(dfObj)

Now modify the list,

# Modify Column Name
columnsNames[0] = 'Test_Name'

This modification will not be reflected in original DataFrame object and DataFrame object will remain same i.e.

   Test  Age      City
a  jack   34    Sydeny
b  Riti   30     Delhi
c  Aadi   16  New York

Change Row Index in DataFrame

Contents of dataframe objects  is as follows,

   Test  Age      City
a  jack   34    Sydeny
b  Riti   30     Delhi
c  Aadi   16  New York

To get the list of all row index names from a dataFrame object, use index attribute instead of columns i.e. df.index.values

# get a list of all the column names 
indexNamesArr = dfObj.index.values

It returns an ndarray of all row indexes in dataframe. Any modification in this ndArray (df.index.values) will modify the actual DataFrame. For example let’s change the name of row index at position 0 i.e.

# Modify a Row Index Name
indexNamesArr[0] = 'P'

This change will be reflected in linked DataFrame object too. Now contents of DataFrame object is,

   Test  Age      City
P  jack   34    Sydeny
b  Riti   30     Delhi
c  Aadi   16  New York

But if we convert it to list before modifying then changes will not be reflected in original DataFrame object. For example create a copy list of Row Index Names of DataFrame i.e.

# get a copy list of all the column names 
indexNames = list(dfObj.index.values)

 

Complete example is as follows :

import pandas as pd

def main():
    
    students = [ ('jack', 34, 'Sydeny') ,
                 ('Riti', 30, 'Delhi' ) ,
                 ('Aadi', 16, 'New York') ]
    
    # Create a DataFrame object
    dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City'], index=['a', 'b', 'c']) 
    
    print("Original DataFrame : " , dfObj, sep="\n");
 
    '''
    Get All Columns Names in DataFrame 
    '''

    # Get ndArray of all column names 
    columnsNamesArr = dfObj.columns.values
    
    print("Column Names : " , columnsNamesArr)

    '''
    Get Columns Name by Index/position in DataFrame 
    '''

    print("Column Names at index 2: " , dfObj.columns.values[2])

    '''
    Pandas : Modify Column Name in DataFrame 
    '''        
    # Modify a Column Name
    columnsNamesArr[0] = 'Test'
    
    print("Modified Column Names : " , columnsNamesArr)
    
    
    print("Modified DataFrame : ")
    print(dfObj)

    '''
    Get Copy of all Columns Names in DataFrame 
    '''
    # get a copy list of all the column names 
    columnsNames = list(dfObj.columns.values)
    
    print("Column Names : " , columnsNames)
    
    # Modify Column Name
    columnsNames[0] = 'Test_Name'
    print("Modified Column Names : " , columnsNames)
    
    print("DataFrame : ")
    print(dfObj)

    '''
    Get List of All Index Names in DataFrame
    '''
    
    # get a list of all the column names 
    indexNamesArr = dfObj.index.values
    
    print("All Index Names : " , indexNamesArr)

    '''
    Get Row Index Names in DataFrame by Poisition
    '''

    print("Row Index Names at position 2: " , dfObj.index.values[2])

    '''
    Pandas : Modify Row Index Name in DataFrame 
    '''        
    # Modify a Row Index Name
    indexNamesArr[0] = 'P'
    
    print("Modified Row Index Names : " , indexNamesArr)
    
    
    print("Modified DataFrame : ", dfObj, sep='\n')

    '''
    Get Copy of all Row Index Names in DataFrame 
    '''
    # get a copy list of all the column names 
    indexNames = list(dfObj.index.values)
    
    print("All Row Index Names : " , indexNames)

    
if __name__ == '__main__':
    main()

Output:

Original DataFrame : 
   Name  Age      City
a  jack   34    Sydeny
b  Riti   30     Delhi
c  Aadi   16  New York
Column Names :  ['Name' 'Age' 'City']
Column Names at index 2:  City
Modified Column Names :  ['Test' 'Age' 'City']
Modified DataFrame : 
   Test  Age      City
a  jack   34    Sydeny
b  Riti   30     Delhi
c  Aadi   16  New York
Column Names :  ['Test', 'Age', 'City']
Modified Column Names :  ['Test_Name', 'Age', 'City']
DataFrame : 
   Test  Age      City
a  jack   34    Sydeny
b  Riti   30     Delhi
c  Aadi   16  New York
All Index Names :  ['a' 'b' 'c']
Row Index Names at position 2:  c
Modified Row Index Names :  ['P' 'b' 'c']
Modified DataFrame : 
   Test  Age      City
P  jack   34    Sydeny
b  Riti   30     Delhi
c  Aadi   16  New York
All Row Index Names :  ['P', 'b', 'c']

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top