Built with Tutify.io

    AIStudyLabs

    How to Rename Specific Columns in Pandas

    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.

    Key Points –

    <a id="h-quick-examples-of-rename-specific-columns"></a>

    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
    

    <a id="h-rename-single-specific-column"></a>

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

    <a id="h-rename-multiple-specific-columns"></a>

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

    <a id="h-rename-specific-columns-using-lambda-function"></a>

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

    <a id="h-rename-specific-column-using-dataframe-columns-str-replace"></a>

    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)
    

    <a id="h-using-the-dataframe-columns-values-attribute"></a>

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

    <a id="h-complete-example-of-rename-specific-columns-in-pandas"></a>

    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)
    

    <a id="h-faq-on-rename-specific-columns-in-pandas"></a>

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

    <a id="h-conclusion"></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 !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References