Pandas DataFrame is a two-dimensional data structure used to store the data in rows and column format and each column will have a header.
You can rename the column in Pandas dataframe using the df.rename( columns={“Old Column Name”:”New Column Name” } ,inplace=True) statement.
In this tutorial, you’ll learn various methods available to rename columns in Pandas DataFrame.
If you’re in Hurry
Use the rename()
method and pass a dictionary to rename columns.
df.rename(
columns={"Old_Column_Name_1":"New_Column_Name_1",
"Old_Column_Name_2":"New_Column_Name_2"}
,inplace=True)
Where Columns
is a dictionary containing a list of columns to be changed or renamed.
Key
– the old column nameValue
– new column nameinplace=True
– denotes to make changes in the current DataFrame directly rather than creating a new Dataframe after renaming the columns
If You Want to Understand Details, Read on…
There are different methods to rename columns in pandas dataframe. Let us learn these methods by using the below sample dataframe df
.
Sample DataFrame
import pandas as pd
data = {
"Lang":["Java","Python","Cobol","Javascript"],
"Difficulty":["Medium","Easy","Hard","Medium"],
"Type":["Statically Typed","Dynamically Typed","NA","Dynamically typed"]
}
df = pd.DataFrame(data)
print(df)
DataFrame Will Look Like
Lang Difficulty Type
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Now, let’s discuss all the methods available to rename columns in pandas.
Using rename() Method
rename() method is used to rename the columns or indexes/rows in the Pandas DataFrame.
Syntax
df.rename(columns={"Old_Column_Name_1":"New_Column_Name_1", "Old_Column_Name_2":"New_Column_Name_2"} ,inplace=True)
Where Columns
is a dictionary containing a list of columns to be changed or renamed.
Key
– the old column nameValue
– new column nameinplace=True
– denotes to make changes in the current DataFrame directly rather than creating a new Dataframe after renaming the columns
Using Columns Attribute
You can change the dataframe column names by directly assigning the column names to the DataFrame using columns attributes as below.
Syntax
df.columns = ['New_Column_1', 'New_Column_2']
This will directly assign the new column names to your dataframe.
Ensure that the list has the same number of values as the number of columns in the dataframe.
Example
#Print the dataframe before renaming
print("Dataframe before Renaming\n\n")
print(df)
#Renaming the dataframe Column Index 0
df.columns = ['Language','Complexity','Typ']
print("\n\nDataframe after Renaming using Columns Attribute\n\n")
print(df)
Output
Dataframe before Renaming
*************************
Lang Difficulty Type
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Dataframe after Renaming using Columns Attribute
***********************************************
Language Difficulty Typ
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Using set_axis() Method
set_axis() method can be used to set the axis or rename columns of the dataframe.
You can set the index name when creating a dataframe using this set_axis()
method.
A pandas DataFrame has two axes. where
- A column axis can be mentioned as
axis='columns'
oraxis=1
- A row axis can be mentioned as
axis='index'
oraxis=0
To rename column axes using the set_Axis()
, use axis =1
or axis = columns
as a parameter.
Syntax
df.set_axis(['New_Column_1', 'New_Column_2'],axis=1,inplace=True)
Where
['New_Column_1', 'New_Column_2']
– list of columns names to be assigned.axis=1
– denotes columns to be renamed.inplace=True
– denotes the actual DataFrame needs to be renamed rather than creating a new DataFrame object.
Example
print("Dataframe before Renaming\n\n")
print(df)
df.set_axis(["Language","Complexity","Typ"],axis=1,inplace=True)
print("\n\nDataframe after Renaming using Columns Attribute\n\n")
print(df)
Output
Dataframe before Renaming
Lang Difficulty Type
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Dataframe after Renaming using Columns Attribute
Language Complexity Typ
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
This is how you can set the column axis
using the set_axis()
method in the pandas dataframe.
Rename all Columns
In this section, you’ll learn how to use rename()
method to change all column names in the DataFrame.
- To rename all columns in pandas DataFrame, pass a dictionary of columns to the rename method.
Syntax
df.rename(
columns={"Old_Column_Name_1":"New_Column_Name_1",
"Old_Column_Name_2":"New_Column_Name_2",
"Old_Column_Name_3":"New_Column_Name_3"}
,inplace=True)
Where Columns
is a dictionary containing a list of columns to be changed or renamed.
Key
should be a old column nameValue
should be a new column nameinplace=True
– Used to denote to make changes in the current DataFrame directory rather creating a new Dataframe for change
Example
print("Dataframe before Renaming\n\n")
print(df)
df.rename(columns={"Lang":"Language","Difficulty":"Complexity","Typ":"Type"},inplace=True)
print("\n\nDataframe after Renaming\n\n")
print(df)
Output
Dataframe before Renaming
Lang Difficulty Type
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Dataframe after Renaming
Language Complexity Typ
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Rename Specific Columns
In this section, you’ll learn how to rename a specific column in pandas DataFrame using the rename()
method.
Syntax
df.rename(
columns={"Old_Column_Name_1":"New_Column_Name_1"}
,inplace=True)
Where Columns
is a dictionary containing a list of columns to be changed or renamed.
Key
should be a old column nameValue
should be a new column nameinplace=True
– Used to denote to make changes in the current DataFrame directory rather creating a new Dataframe for change
Example
print("Dataframe before Renaming\n\n")
print(df)
df.rename(columns={"Lang":"Language"},inplace=True)
print("\n\nDataframe after Renaming the Language Column\n\n")
print(df)
Output
Dataframe before Renaming
Lang Difficulty Type
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Dataframe after Renaming
Language Difficulty Type
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Rename DataFame Column by index
You can rename a column using the column index. This is also known as renaming the pandas column using number or position.
The index is 0 based. Use 0, if you want to rename the first column.
- Use the column name in the dictionary key by fetching the existing column name using its index as
df.columns[0]
.
Syntax
df.rename(columns={df.columns[0]: 'New_Column_name'})
Example
print("Dataframe before Renaming")
print(df)
df.rename(columns={df.columns[0]: 'Language'},inplace=True)
print("\n\nDataframe after Renaming using column Index 0. Lang is renamed as Language\n\n")
print(df)
Output
Dataframe before Renaming
Lang Difficulty Type
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Dataframe after Renaming using column Index 0. Lang is renamed as Language
Language Difficulty Type
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
Handling Errors while using rename function
When you use rename()
method, it’ll raise a KeyError
error when the specified Key
is not available in the DataFrame.
You can specify what needs to be done in case of such errors.
- Use
errors='raise'
to raise the error. - Use
errors='ignore'
to ignore the error and perform rename
operation for valid columns. This is the default behaviour.
Syntax to Raise error
df.rename(
{"Old_Column_Name_1":"New_Column_Name_2","Unavailable_Column":"New_Column_Name_2"},
axis='columns', inplace=True,
errors='raise')
Example
print("Dataframe before Renaming")
print(df)
df.rename({"Langguage":"Language"},axis=1, inplace=True,
errors='raise')
print("\n\nDataframe after Renaming using Axis Keyword\n\n")
print(df)
Output
Dataframe before Renaming
*************************
Lang Difficulty Type
0 Java Medium Statically Typed
1 Python Easy Dynamically Typed
2 Cobol Hard NA
3 Javascript Medium Dynamically typed
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
KeyError: "['Langguage'] not found in axis"
Now, when you try to rename the non-existing column Langguage
, the Pandas rename()
method raises an error.