Polars is a DataFrame library implemented in Rust with bindings for Python. Due to its efficiency and scalability, Polars is particularly well-suited for data manipulation and analysis tasks.
DataFrame.equals()
methodThe DataFrame.equals()
method in Polars is used to check whether two DataFrames are equal. It compares the values in corresponding positions of the two DataFrames and returns a boolean indicating whether they are identical. Optionally, we can specify whether null values should be considered equal.
DataFrame.equals(other: DataFrame,*,null_equal: bool = True,)
Here are the parameters that DataFrame.equals()
function will take:
other
: DataFrame to compare with.
null_equal
(optional): Consider null values as equal (default is True).
It will return a boolean value.
We’ll delve into the practical use of the DataFrame.equals()
method in the Polars library. This method determines if two DataFrames are identical by comparing their values position-wise. We have created two DataFrames, dataframe_1
and dataframe_2
, and we’ll employ the DataFrame.equals()
method to examine their equality.
import polars as pl# Create two DataFramesdataframe_1 = pl.DataFrame({"id": [101, 102, 103],"price": [45.0, 55.0, 65.0],"category": ["electronics", "clothing", "home_goods"],})dataframe_2 = pl.DataFrame({"category": [103, 102, 101],"price": [65.0, 55.0, 45.0],"id": ["home_goods", "clothing", "electronics"],})# Checking if dataframe_1 equals dataframe_1result_1 = dataframe_1.equals(dataframe_1)print(f"dataframe_1 equals dataframe_1: {result_1}")# Checking if dataframe_1 equals dataframe_2, considering null valuesresult_2 = dataframe_1.equals(dataframe_2)print(f"dataframe_1 equals dataframe_2: {result_2}")
Now, let's break down the code. We start by importing the Polars library. Then we created two DataFrames, dataframe_1
and dataframe_2
, with different values. The equals
method is used to compare these DataFrames:
Line 21: We check whether dataframe_1
is equal to itself. Because it is identical, the result will be True
.
Line 25: We checks whether dataframe_1
is equal to dataframe_2
, considering null values as equal. The result will be False
because the values in corresponding positions are not the same, and null values are not considered equal by default.
Understanding the DataFrame.equals()
method is crucial for ensuring data consistency and integrity when working with Polars DataFrames. As we explore the capabilities of the Polars library, keep in mind that this method provides a reliable means of comparing and validating the equality of DataFrames concisely and efficiently.
Free Resources