• Post author:
  • Post category:Polars
  • Post last modified:May 29, 2025
  • Reading time:11 mins read
You are currently viewing 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.

Advertisements

Key Points –

  • Polars column names are standard Python strings, so you can apply Python string methods directly.
  • The capitalize() string method converts the first character to uppercase and the rest to lowercase.
  • Using rename() with a dictionary comprehension is a common way to change column names in Polars.
  • Direct assignment to df.columns with a list comprehension is supported for renaming all columns at once.
  • str.capitalize() capitalizes the first letter but also lowercases the rest of the string, which might be undesirable.
  • Assigning to df.columns modifies column names in-place, while rename() returns a new DataFrame.
  • String slicing with col[:1].upper() + col[1:] is a common technique to capitalize only the first letter.
  • Using df.rename() with a dictionary mapping can rename specific columns selectively.

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.

polars first letter upper case

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,

  • df.rename(str.capitalize) applies the Python built-in string method capitalize to each column name.
  • It changes the first letter to uppercase and the rest of the letters to lowercase.
polars first letter upper case

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,

  • col.capitalize() capitalizes the first character of the string and makes the rest lowercase.
  • The dictionary comprehension creates a mapping from original column names to the capitalized ones.
  • rename() applies this mapping to the DataFrame columns.

Yields the same output as above.

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,

  • df.columns is a list of column names.
  • [col.capitalize() for col in df.columns] creates a new list where; Each column name has its first letter uppercased (e.g., 'courses''Courses').
  • Assigning this list back to df.columns renames all columns.

Yields the same output as above.

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,

  • col[:1].upper() uppercases only the first character.
  • col[1:] keeps the rest of the column name unchanged.
  • Combined 'courses', 'Courses', 'fees', 'Fees', etc.
  • This is different from str.capitalize(), which lowers the rest of the string.

Yields the same output as above.

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,

  • (lambda x: x[:1].upper() + x[1:]) capitalizes only the first character (like before using slicing).
  • {col: lambda(col) for col in df.columns} applies the transformation to each column name.
  • rename(rename_map) applies the new names using the dictionary.

Yields the same output as above.

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

Reference