Pandas provide multiple ways to rename a column in the Pandas DataFrame, throughout this article we are about to explore all the possible ways to rename an existing column in the Pandas DataFrame with the help of the examples so that you don’t have any confusion regarding rename column to Pandas DataFrame.
I have a sample CSV file named employee_dataset.csv along with some dummy records as you can see below.
I have loaded all the records of the CSV file into Pandas DataFrame as you can see below.
👉 Read CSV file into Pandas DataFrame:- Click Here
Now, let’s explore all the possible ways to rename columns in Pandas DataFrame with the help of the example.
Headings of Contents
How to Rename Column Name in Pandas DataFrame
Using the Pandas DataFrame rename() method
rename() is a Pandas DataFrame method that is always used to rename the existing column of the Pandas DataFrame with a new column name. This is one of the very useful methods in Pandas to rename single or multiple columns.
Example:- Rename single column name in Pandas DataFrame
For example, I want to rename the column ‘emp_first_name‘ to ‘Employee First Name‘. To rename a single column we have to pass the old and new column names as Python Dictionary to the columns parameter of the rename() method. Remember key will indicate the old column name and the value indicates the new column name.
import pandas as pd
df = pd.read_csv(
'../../Datasets/employee_dataset.csv'
)
df.rename(columns={
"emp_first_name": "Employee First Name"
}, inplace=True
)
print(df)
Example: Rename Multiple Columns of The Pandas DataFrame
In this example section, I have to rename the multiple columns of the Pandas DataFrame. To rename a single column we have to pass all old and new column names as Python Dictionary to the columns parameter of the rename() method. Remember key will indicate the old column name and the value indicates the new column name.
import pandas as pd
df = pd.read_csv(
'../../Datasets/employee_dataset.csv'
)
df.rename(columns={
"emp_first_name": "Employee First Name",
"emp_last_name": "Employee Last Name",
"emp_gender": "Employee Gender",
}, inplace=True
)
print(df)
By assigning a list of new column names
Columns of Pandas DataFrame can also be renamed by directly assigning the list of column names to the columns property of the Pandas DataFrame.The only disadvantage of this functionality is, that we have to provide all the column names we cannot rename some column names.
So if you want to rename all the column names of the Pandas DataFrame, Then you can go with this.
Example: Rename all the column Names of Pandas DataFrame
import pandas as pd
df = pd.read_csv(
'../../Datasets/employee_dataset.csv'
)
columns = ['Employee ID', 'Employee First Name', 'Employee Last Name', 'Employee Gender',
'Employee Salary', 'Employee Department']
df.columns = columns
print(df)
Using set_axis() Method
The set_axis() is another DataFrame method that is used to rename the column name to the Pandas DataFrame.To rename the columns using the set_axis() method, we have to pass the new column names as a list as a first parameter and column value in the axis parameter of the set_axis() method.
Let’s rename column names as the first parameter.
Example: Rename column name in Pandas DataFrame using set_axis() method
import pandas as pd
df = pd.read_csv(
'../../Datasets/employee_dataset.csv'
)
columns = ['Employee ID', 'Employee First Name', 'Employee Last Name', 'Employee Gender',
'Employee Salary', 'Employee Department']
new_df = df.set_axis(columns, axis='columns')
print(new_df)
Useful Pandas Tutorials:
- How To Add a Column in Pandas Dataframe
- How to Replace Column Values in Pandas DataFrame
- How to Convert Excel to JSON in Python
- How to convert DataFrame to HTML in Python
- How to Delete a Column in Pandas DataFrame
- How to convert SQL Query Result to Pandas DataFrame
- How to Convert Dictionary to Excel in Python
- How to Convert Excel to Dictionary in Python
- How to Rename Column Name in Pandas DataFrame
Conclusion
Throughout this article, we have seen multiple ways to rename column names in the Pandas DataFrame. But I will recommend, that if you want to rename all the column names then all the methods will be fine and for some columns, you can go with the first one which was the rename() method.
If you find this article helpful, please share and keep visiting for further Pandas Tutorials.
Thanks for your valuable time…