If you’re working with data in Python using Pandas library, you might come across a scenario where you need to rename columns of a DataFrame. This is a common task in data cleaning and preprocessing, as well as data analysis and visualization. Fortunately, Pandas provides a simple and flexible way to rename columns in a DataFrame. In this article, we’ll explore different methods to rename columns in Pandas DataFrame.
Method 1: Using the rename() Method
The simplest way to rename columns in Pandas DataFrame is to use the rename()
method. This method allows you to pass a dictionary that maps old column names to new column names. Here’s an example:
import pandas as pd # create a sample dataframe data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'gender': ['F', 'M', 'M']} df = pd.DataFrame(data) # rename columns using dictionary new_columns = {'name': 'Full Name', 'age': 'Age', 'gender': 'Gender'} df = df.rename(columns=new_columns) print(df)
In this example, we create a sample DataFrame with three columns: name
, age
, and gender
. Then, we define a dictionary that maps old column names to new column names, and pass it to the rename()
method. Finally, we print the updated DataFrame with renamed columns.
Method 2: Using the columns Attribute
Another way to rename columns in Pandas DataFrame is to directly modify the columns
attribute. This attribute is a list that contains the column names of the DataFrame, and can be modified by assigning a new list of column names. Here’s an example:
import pandas as pd # create a sample dataframe data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'gender': ['F', 'M', 'M']} df = pd.DataFrame(data) # rename columns using columns attribute df.columns = ['Full Name', 'Age', 'Gender'] print(df)
In this example, we create a sample DataFrame with three columns: name
, age
, and gender
. Then, we modify the columns
attribute of the DataFrame by assigning a new list of column names. Finally, we print the updated DataFrame with renamed columns.
Renaming Columns in Pandas DataFrame
When working with data, it is common to encounter datasets where the column names are not in a format that is easy to work with or even understand. In such cases, it is important to rename the columns in a way that makes sense to the user. This is where the rename()
function in Pandas comes in handy.
The rename()
function can be used to rename the columns of a DataFrame in Pandas. It takes a dictionary as an argument, where the keys represent the old column names and the values represent the new column names. The rename()
function returns a new DataFrame with the updated column names.
import pandas as pd data = {'name': ['John', 'Peter', 'Sarah'], 'age': [25, 30, 28], 'gender': ['Male', 'Male', 'Female']} df = pd.DataFrame(data) df.rename(columns={'name': 'Name', 'age': 'Age', 'gender': 'Gender'}, inplace=True) print(df)
In the code above, we first create a sample DataFrame with three columns: name
, age
, and gender
. We then use the rename()
function to rename the columns to Name
, Age
, and Gender
. Finally, we display the updated DataFrame using the print()
function.
It is worth noting that the rename()
function returns a new DataFrame with the updated column names. If you want to modify the original DataFrame, you need to set the inplace
parameter to True
.
Rename Single Column
To rename a single column in a Pandas DataFrame, you can use the rename()
method. Here is an example:
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) # rename the 'Name' column to 'First Name' df = df.rename(columns={'Name': 'First Name'}) print(df)
Output:
First Name Age 0 Alice 25 1 Bob 30 2 Charlie 35
Rename Multiple Columns
If you want to rename multiple columns, you can pass a dictionary with old and new names to the rename()
method. Here is an example:
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Salary': [50000, 60000, 70000]} df = pd.DataFrame(data) # rename the 'Name' column to 'First Name' and 'Salary' column to 'Income' df = df.rename(columns={'Name': 'First Name', 'Salary': 'Income'}) print(df)
Output:
First Name Age Income 0 Alice 25 50000 1 Bob 30 60000 2 Charlie 35 70000
Rename Columns in Place
By default, the rename()
method returns a new DataFrame with the renamed columns. If you want to rename the columns in place, you can set the inplace
parameter to True
.
import pandas as pd create a sample dataframe data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'Salary': [50000, 60000, 70000]} df = pd.DataFrame(data) # rename the 'Name' column to 'First Name' # and 'Salary' column to 'Income' in place df.rename(columns={'Name': 'First Name', 'Salary': 'Income'}, inplace=True) print(df)
We can also use the rename() method to rename multiple columns at once. In the example below, we will rename the “First Name” and “Age” columns to “Name” and “Years”, respectively:
import pandas as pd Creating a sample dataframe data = {'First Name': ['John', 'Jane', 'Bob'], 'Last Name': ['Doe', 'Doe', 'Smith'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) Renaming multiple columns df = df.rename(columns={'First Name': 'Name', 'Age': 'Years'}) print(df)
Output:
Name Last Name Years 0 John Doe 25 1 Jane Doe 30 2 Bob Smith 35
We can also use the columns attribute to directly assign a list of new column names to the dataframe’s columns:
import pandas as pd Creating a sample dataframe data = {'First Name': ['John', 'Jane', 'Bob'], 'Last Name': ['Doe', 'Doe', 'Smith'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) Assigning new column names df.columns = ['Name', 'Surname', 'Years'] print(df)
Output:
Name Surname Years 0 John Doe 25 1 Jane Doe 30 2 Bob Smith 35
Last Updated on May 12, 2023 by admin