Python | Change column names and row indexes in Pandas DataFrame

16 May, 2020
Given a Pandas DataFrame, let’s see how to change its column names and row indexes.
About Pandas DataFramePandas 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 librariesimport pandas as pd   # Create a dataFrame using dictionarydf=pd.DataFrame({"Name":['Tom','Nick','John','Peter'],                 "Age":[15,26,17,28]})  # Creates a dataFrame with# 2 columns and 4 rowsdf
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 namesdf.columns =['Col_1', 'Col_2']  # Change the row indexesdf.index = ['Row_1', 'Row_2', 'Row_3', 'Row_4']  # printing the data framedfMethod #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"})  dfChange 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')  dfMethod #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 functiondf = df.rename(columns=lambda x: x+'x')  # this will modify all the column namesdfMethod #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 columndfLet’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 = lambda x: x + 10)  dfNow, 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. df
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 namesdf.columns =['Col_1', 'Col_2']  # Change the row indexesdf.index = ['Row_1', 'Row_2', 'Row_3', 'Row_4']  # printing the data framedf
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 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"})  dfChange 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
# 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 functiondf = df.rename(columns=lambda x: x+'x')  # this will modify all the column namesdf
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 namesdf
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 columndfLet’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 = lambda x: x + 10)  dfNow, 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. df
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 = lambda x: 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 = 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
Shubham__Ranjan
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Technical Scripter 2018
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Convert integer to string in Python