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 of how 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 quick example
# 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 results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


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.


    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 param for columns you wanted to rename. So you just pass a dict with key-value pair; the key is an existing column you would like to rename and 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.


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 wanted to rename in a dictionary mapping. Use inplace=True if you wanted to rename an existing DataFrame object. When you use specify 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.


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. The below example add’s ‘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.


Index(['ren_Courses', 'ren_Fee', 'ren_Duration', 'ren_Discount'], dtype='object')

5. Rename Specific Column Using Dataframe.columns.str.replace()

Similar to replace() method of strings in python, pandas Index and Series (object dtype 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.


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.


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)

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 !!

You May Also Like

References

Leave a Reply

You are currently viewing How to Rename Specific Columns in Pandas