How to rename the column names in Polars Python

Polars is a Rust-based DataFrame library with Python bindings. It offers high-performance data processing for large datasets. It excels in parallel processing and supports various data sources like CSV, Parquet, and Arrow, making it ideal for efficient tabular data management.

In this Answer, we’ll discuss the rename() function of the Polars library.

The rename() function

The rename() function accepts key-value pairs that map from old to new names. It assists us in simply changing the column names by just passing the old column name and the new one, as mentioned in the syntax below:

Syntax

Here’s the syntax for the rename() function:

DataFrame.rename(oldColumnName : newColumnName) → DataFrame

It will return a DataFrame with updated column names on successfully executing this function.

Code

Let’s start with importing the polars library and giving it the alias pl to make it easier to refer to the code in the following way:

import polars as pl

Here’s the coding example of the rename() function:

import polars as pl
students_df = pl.DataFrame(
{
"name": ["John", "Ria", "Steve", "Jordan", "Sara"],
"age": [19, 18, 20, 19, 22],
"subjects": ["Geography", "Biology", "Physics", "Chemistry", "Math"],
"score": [89, 92, 99, 82, 94]
}
)
print("- Display the original DataFrame\n", students_df)
print("- Display DataFrame after rename columns:\n\tsubjects --> domains\n\tscore --> numbers\n",
students_df.rename({"score":"numbers", "subjects":"domains"}))

Code explanation

Let’s discuss the above code in detail.

  • Line 3: We create the DataFrame named df.

  • Lines 3–10: We initialize the DataFrame with four columns: "name", "age", "subjects", and "score". Each column consists of sample data of a few students.

  • Line 12: We display the created DataFrame.

  • Line 13: We call the rename() function to change the "subjects" column name to "domains" and "score" column name to "numbers" and display the updated DataFrame.

The rename() function in the Polars library is valuable for changing column names in the chaining method performed in DataFrame operations.

Unlock your potential: Polars in Python series, all in one place!

To continue your exploration of Polars, check out our series of Answers below:

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved