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.
rename()
functionThe 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:
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.
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 plstudents_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"}))
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:
How to scale and normalize data in Python using Polars
Learn how to transform raw data using Python's Polars library to scale it (0-1) and normalize it (mean 0, std 1).
What is DataFrame.clear function in Polars Python?
Learn how to use Polars' DataFrame.clear()
to create a null-filled copy, either empty if n=0
or with n
null rows.
How to reverse a DataFrame in Polars Python?
Learn how to use Polars, a Rust-based DataFrame library for Python, which offers a reverse()
function to efficiently revert DataFrame rows, providing an alternative to pandas.
How to rename the column names in Polars Python?
Learn how to use Polars' rename()
function to efficiently rename DataFrame columns using key-value pairs, enhancing data management and processing.
What is Polars library in Python?
Learn how Polars, a fast DataFrame library in Rust for Python, offers high-performance data manipulation and analysis similar to Pandas.
How to concatenate two Dataframes in Polars Python
Learn how Polars, leveraging Rust, offers efficient DataFrame concatenation in Python with the concat()
method.
How to perform a transpose of a Python Polars DataFrame
Learn how to use Polars' DataFrame.transpose()
to efficiently transpose DataFrames, with options for including headers and custom column names, enhancing data manipulation capabilities.
How to check the polars version in Python
Learn how to ensure the correct Polars version by using pip3 show polars
or by printing pl.__version__
in Python.
What is DataFrame.update function in Polars Python?
Learn how to use the update()
function in Polars to merge two DataFrames, updating the target with non-null values from the source, and supporting various join strategies.
Free Resources