• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:9 mins read
You are currently viewing How to Rename Specific Columns in Pandas

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.

1. 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.


# Below are some quick examples

# 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

2. 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')

3. 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')

4. 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')

5. 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)

6. 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')

7. 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)

8. Conclusion

In this article, you have learned how to rename specific columns in pandas DataFrame using DataFrame.renam(), DataFrame.columns.str.replace(), and DataFrame.columns.values[] function with examples.

Happy Learning !!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply