Pandas: Checking the equality of 2 Series (element-wise)

Updated: February 20, 2024 By: Guest Contributor Post a comment

Overview

In data analysis, comparing two datasets for equality is a common task. It aids in identifying differences, confirming data transformations, or ensuring consistency across data sources. Pandas, a powerful Python library designed for data manipulation and analysis, provides an easy-to-use interface for comparing two Series objects element-wise. This tutorial will guide you through multiple methods to check the equality of two Pandas Series, from basic to advanced techniques, complete with code examples and outputs.

Preparation

Before delving into comparisons, let’s briefly introduce what Pandas Series are. A Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). The axis labels are collectively referred to as the index. The basic method to create a Series is to use pd.Series(data, index), where data can be a list, ndarray, etc., and index is a list of axis labels.

Let’s create 2 sample Pandas Series to use in the coming examples:

import pandas as pd

# Creating a simple series
s1 = pd.Series([1, 2, 3, 4])
s2 = pd.Series([1, 2, 3, 6])

Basic Comparison Using equals() Method

The most straightforward way to compare two Series for equality is using the equals() method. This method checks if two Series have the same shape and elements, and returns a boolean value.

s1.equals(s2)
# Output: False

Element-wise Comparison Using == Operator

For element-wise comparison, the equality == operator can be used. This operation compares corresponding elements in the two Series and returns a new Series of boolean values indicating the result of the comparison for each element.

result = s1 == s2
print(result)
# Output:
# 0     True
# 1     True
# 2     True
# 3    False
# dtype: bool

Handling NaN Values in Comparisons

In data analysis, NaN (Not a Number) values are commonplace. However, traditional comparison methods treat NaN values as unequal, even when comparing NaN to NaN. To handle this, Pandas provides the pd.Series.equals() method, which can accurately compare NaN values. However, for element-wise comparison, another approach is required.

The pd.isna() function can be used in tandem with the == operator to properly compare Series containing NaN values.

s3 = pd.Series([1, NaN, 3, 4])
s4 = pd.Series([1, NaN, 3, 5])

result_with_nan = (s3 == s4) | (pd.isna(s3) & pd.isna(s4))
print(result_with_nan)
# Output:
# 0     True
# 1     True
# 2     True
# 3    False
# dtype: bool

Using the compare() Method for Detailed Comparison

For a more detailed element-wise comparison, Pandas introduced the compare() method in version 1.1.0. This method provides a side-by-side DataFrame showing differences between the Series, highlighting where they do not match.

comparison = s1.compare(s2)
print(comparison)
# Output:
#    self  other
# 3     4      6

Advanced Techniques: Custom Comparison Functions

For more complex comparisons, you might need to define custom logic. Pandas allows for this through the use of vectorized operations and the apply() method. Here, you can apply any function to the elements of the Series, thus enabling highly customized comparison logic.

An example might involve comparing Series elements within a certain tolerance level.

import numpy as np

def within_tolerance(x, y, tol=0.1):
    return np.abs(x - y) <= tol

# Create Series
s5 = pd.Series([1.0, 2.1, 3.2, 4.1])
s6 = pd.Series([1.1, 2.0, 3.1, 4.2])

custom_result = s5.apply(lambda x: within_tolerance(x, s6[s5.index.get_loc(x)], tol=0.15))
print(custom_result)
# Output:
# 0     True
# 1     True
# 2     True
# 3     True
# dtype: bool

Conclusion

Pandas provides several efficient methods for comparing two Series element-wise, from basic equality checks to detailed comparisons and custom logic implementation. Understanding these methods enables data analysts and scientists to verify data integrity and consistency accurately and effortlessly.