To rename specific columns in pandas DataFrame use rename()
method. In this article, I will explain several ways to rename a single specific column and multiple columns of the Pandas DataFrame using functions like DataFrame.rename()
, DataFrame.columns.str.replace()
, DataFrame.columns.values[]
, lambda
function, and more with examples.
Key Points –
- Use the
rename()
function to rename specific columns in a DataFrame. - Pass a dictionary to the
columns
parameter ofrename()
where the keys are current column names and the values are the new names. - The
rename()
function returns a new DataFrame with the updated column names, leaving the original DataFrame unchanged unlessinplace=True
is specified. - Setting the
inplace=True
parameter modifies the original DataFrame without creating a copy. - Use the
str.replace()
method to rename columns based on a pattern, applying it to thecolumns
attribute of the DataFrame. - The
columns
attribute can be directly assigned a list of new column names if renaming all columns at once.
Quick Examples of Rename Specific Columns
If you are in a hurry, below are some quick examples of how to rename specific columns in pandas.
# Quick examples of rename specific columns
# Rename single specific column
df.rename(columns = {"Courses":"Courses_List"}, inplace = True)
# Rename multiple specific columns
df.rename({"Courses":"Courses_List", "Fee":"Courses_Fees", "Duration":"Courses_Duration", "Discount":"Courses_Discount"}, axis = "columns",inplace = True)
# Rename specifi columns
# Using lambda function
df.rename(columns=lambda x: 'ren_'+x, inplace=True)
# Using String replace()
# To rename specific column
df.columns = df.columns.str.replace("Duration","Course_Duration")
# Rename specific all column names
df.columns = df.columns.str.replace(' ', '_')
# Using the values attribute
df.columns.values[3] = "Courses_Discount"
Now, let’s create a DataFrame with a few rows and columns, execute these examples, and validate the results. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
# Create DataFrame
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas"],
'Fee' :[20000,25000,22000,30000],
'Duration':['30days','40days','35days','50days'],
'Discount':[1000,2300,1200,2000]
}
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)
Yields below output.
# Output:
Courses Fee Duration Discount
r1 Spark 20000 30days 1000
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 1200
r4 pandas 30000 50days 2000
Rename Single Specific Column
DataFrame.rename() accepts a dictionary as a parameter for columns you want to rename. So you just pass a dict with a key-value pair; the key is an existing column you would like to rename and the value would be your preferred column name.
# Rename single specific column
df.rename(columns = {"Courses":"Courses_List"}, inplace = True)
print(df.columns)
Yields below output.
# Output:
Index(['Courses_List', 'Fee', 'Duration', 'Discount'], dtype='object')
Rename Multiple Specific Columns
You can also use the same approach to rename multiple specific or all columns of pandas DataFrame. All you need to specify multiple columns you want to rename in a dictionary mapping. Use inplace=True
if you want to rename an existing DataFrame object. When you use specific columns param, as shown above, you don’t have to use an axis
param.
# Rename multiple specific columns
df.rename({"Courses":"Courses_List", "Fee":"Courses_Fees", "Duration":"Courses_Duration", "Discount":"Courses_Discount"}, axis = "columns",inplace = True)
print(df.columns)
Yields below output.
# Output:
Index(['Courses_List', 'Courses_Fees', 'Courses_Duration', 'Courses_Discount'], dtype='object')
Rename Specific Columns Using Lambda Function
Another approach would be using the lambda function. In the below example add ‘ren_
’ string to all column names.
# Rename specifi columns using Lambda function
df.rename(columns=lambda x: 'ren_'+x, inplace=True)
print(df.columns)
Yields below output.
# Output:
Index(['ren_Courses', 'ren_Fee', 'ren_Duration', 'ren_Discount'], dtype='object')
Rename Specific Column Using Dataframe.columns.str.replace()
Similar to the replace()
method of strings in Python, pandas Index and Series (object type only) define a (“vectorized”) str.replace
method for string and regex-based replacement. For example syntax: df.columns.str.replace("Duration","Course_Duration")
, replaces 'Duration'
column with 'course_Duration'
.
# Using String replace() to rename specific column
df.columns = df.columns.str.replace("Duration","Course_Duration")
print(df.columns)
Yields below output.
# Output:
Index(['Courses', 'Fee', 'Course_Duration', 'Discount'], dtype='object')
To replace all column names.
# Rename specific all column names
df.columns = df.columns.str.replace(' ', '_')
print(df.columns)
Using the DataFrame.
columns.values Attribute
You can use values
attribute on the column you want to rename. This renames a specific column by index.
# Using the values attribute
df.columns.values[3] = "Courses_Discount"
print(df.columns)
Yields below output.
# Output:
Index(['Courses', 'Fee', 'Duration', 'Courses_Discount'], dtype='object')
Complete Example of Rename Specific Columns in Pandas
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas"],
'Fee' :[20000,25000,22000,30000],
'Duration':['30days','40days','35days','50days'],
'Discount':[1000,2300,1200,2000]
}
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)
# Rename single specific column
df.rename(columns = {"Courses":"Courses_List"}, inplace = True)
print(df.columns)
# Rename multiple specific columns
df.rename({"Courses":"Courses_List", "Fee":"Courses_Fees", "Duration":"Courses_Duration", "Discount":"Courses_Discount"}, axis = "columns",inplace = True)
print(df.columns)
# Rename specifi columns
# Using Lambda function
df.rename(columns=lambda x: 'ren_'+x, inplace=True)
print(df.columns)
# Using String replace()
# To rename specific column
df.columns = df.columns.str.replace("Duration","Course_Duration")
print(df.columns)
# Rename specific all column names
df.columns = df.columns.str.replace(' ', '_')
print(df.columns)
# Using the values attribute
df.columns.values[3] = "Courses_Discount"
print(df.columns)
FAQ on Rename Specific Columns in Pandas
To rename specific columns in a DataFrame, you can use the rename()
method. The syntax requires passing a dictionary where the keys are the current column names and the values are the new column names.
You can rename columns in place by setting the inplace=True
argument in the rename()
method. This will modify the original DataFrame without needing to assign it to a new variable.
If you want to rename all columns, you can directly assign a new list of column names to the columns
attribute.
You can rename columns dynamically by using list comprehension or applying a function to the column names. For example, to add a suffix to all columns that contain the letter ‘A’.
Conclusion
In this article, you have learned how to rename specific columns in Pandas DataFrame using DataFrame.rename()
, DataFrame.columns.str.replace()
, and DataFrame.columns.values[]
function with examples.
Happy Learning !!
Related Articles
- How to Reshape Pandas Series?
- Pandas Remove Elements From Series
- Pandas Write DataFrame to CSV File
- Pandas Rename Index of DataFrame
- Pandas Handle Missing Data in Dataframe
- Pandas Rename Index Values of DataFrame
- How to Rename Columns With List in Pandas
- Check If a Column Exists in Pandas DataFrame
- Create Pandas DataFrame With Working Example
- Change Column Data Type On Pandas DataFrame
- Retrieve Number of Rows From Pandas DataFrame
- Pandas Merge Indicator Explained With Examples