Built with Tutify.io

    AIStudyLabs

    How to Change the First Letter of the Column Name to Uppercase in Polars?

    How to Change the First Letter of the Column Name to Uppercase in Polars?

    In Polars, changing the first letter of each column name to uppercase refers to modifying all column names so that they start with a capital letter. The remaining characters in each name may either remain as they are or be transformed, based on the string method you apply. Essentially, it's about renaming columns for consistent formatting by capitalizing their initial character. In this article, I will explain how to change the first letter of the column name to uppercase in Polars.

    Key Points –

    <a id="h-usage-of-change-the-first-letter-of-the-column-name-to-upper-case"></a>

    Usage of Change the First Letter of the Column Name to Upper Case

    You can capitalize the first letter of each column name in a Polars DataFrame by applying Python’s built-in capitalize() method to each column name using a dictionary comprehension with the rename() method.

    To run some examples of how to change the first letter of the column name to upper case in Polars, let’s create a Polars DataFrame.

    
    import polars as pl
    
    # Creating a new Polars DataFrame
    technologies = {
        'courses': ["Spark", "Hadoop", "Hyperion", "Pandas"],
        'fees': [20000, 25000, 30000, 40000],
        'duration': ['30days', '50days', '40days', '60days'],
        'discount': [1000, 1500, 1200, 2500]
    }
    
    df = pl.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    You can apply str.capitalize with Polars’ rename() method to make the first letter of each column name uppercase.

    
    # Rename columns by applying str.capitalize to each column name
    df2 = df.rename(str.capitalize)
    print("DataFrame with capitalized column names:\n", df2)
    

    Here,

    <a id="h-using-dictionary-comprehension-with-rename"></a>

    Using Dictionary Comprehension with rename()

    You can use dictionary comprehension along with the rename() method to capitalize the first letter of each column name. Since column names are just Python strings, you can leverage the built-in capitalize() method to transform them, making the first character uppercase and converting the rest to lowercase.

    
    # Capitalize the first letter of each column using dictionary comprehension
    df2 = df.rename({col: col.capitalize() for col in df.columns})
    print("DataFrame with capitalized column names:\n", df2)
    

    Here,

    Yields the same output as above.

    <a id="h-using-df-columns-assignment-with-list-comprehension"></a>

    Using df.columns Assignment with List Comprehension

    You can capitalize the first letter of each column name in a Polars DataFrame by assigning a new list of column names to df.columns using a list comprehension.

    
    # Capitalize the first letter of each column using list comprehension
    df.columns = [col.capitalize() for col in df.columns]
    print("DataFrame with capitalized column names:\n", df)
    

    Here,

    Yields the same output as above.

    <a id="h-using-slicing-for-more-control-only-first-letter-uppercased"></a>

    Using Slicing for More Control (only First Letter Uppercased)

    If you want full control, such as capitalizing only the first character of each column name and leaving the rest of the name unchanged, you can use string slicing.

    
    # Capitalize only the first letter using slicing
    df.columns = [col[:1].upper() + col[1:] for col in df.columns]
    print("DataFrame with custom capitalized column names:\n", df)
    

    Here,

    Yields the same output as above.

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

    Using a Lambda Function Inside rename()

    You can use a lambda function within the rename() method in Polars by creating a mapping dictionary dynamically through a comprehension that applies the lambda to each column name. For example, to capitalize the first letter of each column name, you can use a lambda function like this inside rename().

    
    # Rename using a lambda function with str slicing
    df2 = df.rename(lambda col: col[:1].upper() + col[1:])
    print("DataFrame with capitalized column names using lambda:\n", df2)
    
    # Use a lambda function to transform column names (e.g., capitalize the first letter)
    rename_map = {col: (lambda x: x[:1].upper() + x[1:])(col) for col in df.columns}
    df2 = df.rename(rename_map)
    print("DataFrame with capitalized column names using lambda:\n", df2)
    

    Here,

    Yields the same output as above.

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

    Conclusion

    In summary, Polars offers flexible ways to rename columns, whether by using string methods like capitalize(), string slicing for precise control, or lambda functions within rename() combined with comprehensions. These approaches let you efficiently customize column names to fit your specific formatting needs.

    Happy Learning!!

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

    Related Articles

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

    Reference