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 df=pd.DataFrame({"Name":['Tom','Nick','John','Peter'], "Age":[15,26,17,28]}) # Creates a dataFrame with # 2 columns and 4 rows df |

- Method #1: Changing the column name and row index using
df.columnsanddf.indexattribute.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 itdf.index=['row1', 'row2', 'row3', ......].# Let's rename already created dataFrame.# Check the current column names# using "columns" attribute.# df.columns# Change the column namesdf.columns=['Col_1','Col_2']# Change the row indexesdf.index=['Row_1','Row_2','Row_3','Row_4']# printing the data framedf
- 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() functiondf=df.rename(columns={"Col_1":"Mod_col"})df
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')df
- 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=lambdax: x+'x')# this will modify all the column namesdf
- Method #4 : Using
valuesattribute 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 columndf
Let’s change the row index using the Lambda function.
# To change the row indexesdf=pd.DataFrame({"A":['Tom','Nick','John','Peter'],"B":[25,16,27,18]})# this will increase the row index value by 10 for each rowdf=df.rename(index=lambdax: x+10)df
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=lambdax: x+5,columns=lambdax: x+'x')# increase all the row index label by value 5# append a value 'x' at the end of each column name.df
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.

