Python | Change column names and row indexes in Pandas DataFrame

Given a Pandas DataFrame, let’s see how to change its column names and row indexes.

About Pandas DataFrame
Pandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame.

  • It consists of rows and columns.
  • Each row is a measurement of some instance while column is a vector which contains data for some specific attribute/variable.
  • Each dataframe column has a homogeneous data throughout any specific column but dataframe rows can contain homogeneous or heterogeneous data throughout any specific row.
  • Unlike two dimensional array, pandas dataframe axes are labeled.

Pandas Dataframe type has two attributes called ‘columns’ and ‘index’ which can be used to change the column names as well as the row indexes.

Create a DataFrame using dictionary.

filter_none

edit
close

play_arrow

link
brightness_4
code

# first import the libraries
import pandas as pd
   
# Create a dataFrame using dictionary
df=pd.DataFrame({"Name":['Tom','Nick','John','Peter'],
                 "Age":[15,26,17,28]})
  
# Creates a dataFrame with
# 2 columns and 4 rows
df

chevron_right


  1. Method #1: Changing the column name and row index using df.columns and df.index attribute.

    In order to change the column names, we provide a Python list containing the names for column df.columns= ['First_col', 'Second_col', 'Third_col', .....].
    In order to change the row indexes, we also provide a python list to it df.index=['row1', 'row2', 'row3', ......].

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Let's rename already created dataFrame.
      
    # Check the current column names
    # using "columns" attribute.
    # df.columns
      
    # Change the column names
    df.columns =['Col_1', 'Col_2']
      
    # Change the row indexes
    df.index = ['Row_1', 'Row_2', 'Row_3', 'Row_4']
      
    # printing the data frame
    df

    chevron_right

    
    

  2. Method #2: Using rename() function with dictionary to change a single column
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # let's change the first column name
    # from "A" to "a" using rename() function
    df = df.rename(columns = {"Col_1":"Mod_col"})
      
    df

    chevron_right

    
    

    Change multiple column names simultaneously –

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # We can change multiple column names by 
    # passing a dictionary of old names and 
    # new names, to the rename() function.
    df = df.rename({"Mod_col":"Col_1","B":"Col_2"}, axis='columns')
      
    df

    chevron_right

    
    

  3. Method #3: Using Lambda Function to rename the columns.

    A lambda function is a small anonymous function which can take any number of arguments, but can only have one expression. Using lambda function we can modify all of the column names at once. Let’s add ‘x’ at the end of each column name using lambda function

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    df = df.rename(columns=lambda x: x+'x')
      
    # this will modify all the column names
    df

    chevron_right

    
    

    Let’s change the row index using Lambda function.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # To change the row indexes
    df = pd.DataFrame({"A":['Tom','Nick','John','Peter'],
                       "B":[25,16,27,18]})
      
    # this will increase the row index value by 10 for each row
    df = df.rename(index = lambda x: x + 10)
      
    df

    chevron_right

    
    

    Now, if we want to change the row indexes and column names simultaneously, then it can be achieved using rename() function and passing both column and index attribute as parameter.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    df = df.rename(index = lambda x: x + 5,
                   columns = lambda x: x +'x')
       
    # increase all the row index label by value 5
    # append a value 'x' at the end of each column name. 
    df

    chevron_right

    
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.