Skip to content
Related Articles

Related Articles

Python | Change column names and row indexes in Pandas DataFrame

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 16 May, 2020
Improve Article
Save Article

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.

# first import the libraries
import pandas as pd
# Create a dataFrame using dictionary
# Creates a dataFrame with
# 2 columns and 4 rows

  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', ......].

    # 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

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

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

    Change multiple column names simultaneously –

    # 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')

  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 the 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

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

  4. Method #4 : Using values attribute to rename the columns.

    We can use values attribute directly on the column whose name we want to change.

    df.columns.values[1] = 'Student_Age'
    # this will modify the name of the first column

    Let’s change the row index using the Lambda function.

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

    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 the parameter.

    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. 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!