Selected Reading

Python Pandas - Binary Comparison Operations



Binary comparison operations in Pandas are used to compare elements in a Pandas Data structure such as, Series or DataFrame objects with a scalar value or another Data structure. These operations return Boolean results that indicate the outcome of each comparison, and these operations are useful for for filtering, condition-based operations, and data analysis.

In this tutorial, you will learn how to perform binary comparison operations like less than, greater than, equal to, and others, on a Pandas Data structure with scalar values and between other DataFrames/Series objects.

Binary Comparison Operators in Pandas

Binary comparison operators are used to compare elements in a Pandas Series or DataFrame with a scalar value. The result of these operations is a boolean Data structure where True indicates the given condition is satisfied and False for not.

Here is a list of common binary comparison operators that can be used on a Pandas DataFrame or Series −

  • <: Checks if each element is less than the given value.

  • >: Checks if each element is greater than the given value.

  • <=: Checks if each element is less than or equal to the given value.

  • >=: Checks if each element is greater than or equal to the given value.

  • ==: Checks if each element is equal to the given value.

  • !=: Checks if each element is not equal to the given value.

Example

The following example demonstrates how to apply comparison operators to a Pandas DataFrame with a scalar value.

import pandas as pd

# Create a sample DataFrame
data = {'A': [1, 5, 3, 8], 'B': [4, 6, 2, 9]}
df = pd.DataFrame(data)

# Display the input DataFrame
print("Input DataFrame:\n", df)

# Perform binary comparison operations
print("\nLess than 5:\n", df < 5)
print("\nGreater than 5:\n", df > 5)
print("\nLess than or equal to 5:\n", df <= 5)
print("\nGreater than or equal to 5:\n", df >= 5)
print("\nEqual to 5:\n", df == 5)
print("\nNot equal to 5:\n", df != 5)

Output

Following is the output of the above code −

Input DataFrame:
    A  B
0  1  4
1  5  6
2  3  2
3  8  9

Less than 5:
        A      B
0   True   True
1  False  False
2   True   True
3  False  False

Greater than 5:
        A      B
0  False  False
1  False   True
2  False  False
3   True   True

Less than or equal to 5:
        A      B
0   True   True
1   True  False
2   True   True
3  False  False

Greater than or equal to 5:
        A      B
0  False  False
1   True   True
2  False  False
3   True   True

Equal to 5:
        A      B
0  False  False
1   True  False
2  False  False
3  False  False

Not equal to 5:
        A     B
0   True  True
1  False  True
2   True  True
3   True  True

Binary Comparison Functions in Pandas

In addition to the above operators, Pandas provides various functions to perform binary comparison operations on Pandas Data structure, by providing the additional options for customization, like selecting the axis and specifying levels for the MultiIndex objects.

Following is the list of binary comparison functions in Pandas −

S.No Function Description
1 lt(other[, axis, level]) Element-wise less than comparison.
2 gt(other[, axis, level]) Element-wise greater than comparison.
3 le(other[, axis, level]) Element-wise less than or equal comparison.
4 ge(other[, axis, level]) Element-wise greater than or equal comparison.
5 ne(other[, axis, level]) Element-wise not equal comparison.
6 eq(other[, axis, level]) Element-wise equal comparison.

Example: Binary Comparison Operations on Pandas Series

This example demonstrates the applying the binary comparison functions between a Pandas Series and a scalar value.

import pandas as pd

# Create a Pandas Series
s = pd.Series([10, 20, 30, 40, 50])

# Display the Series
print("Pandas Series:\n", s)

# Perform comparison operations
print("\nLess than 25:\n", s.lt(25))
print("\nGreater than 25:\n", s.gt(25))
print("\nLess than or equal to 30:\n", s.le(30))
print("\nGreater than or equal to 40:\n", s.ge(40))
print("\nNot equal to 30:\n", s.ne(30))
print("\nEqual to 50:\n", s.eq(50))

Output

Following is the output of the above code −


Pandas Series:
 0    10
1    20
2    30
3    40
4    50
dtype: int64

Less than 25:
 0     True
1     True
2    False
3    False
4    False
dtype: bool

Greater than 25:
 0    False
1    False
2     True
3     True
4     True
dtype: bool

Less than or equal to 30:
 0     True
1     True
2     True
3    False
4    False
dtype: bool

Greater than or equal to 40:
 0    False
1    False
2    False
3     True
4     True
dtype: bool

Not equal to 30:
 0     True
1     True
2    False
3     True
4     True
dtype: bool

Equal to 50:
 0    False
1    False
2    False
3    False
4     True
dtype: bool

Example: Binary Comparison Operations on Pandas DataFrame

Similarly above example, this will perform binary comparison operations between a DataFrame and a scalar value using the binary comparison functions in Pandas.

import pandas as pd

# Create a DataFrame
data = {'A': [10, 20, 30], 'B': [40, 50, 60]}
df = pd.DataFrame(data)

# Display the DataFrame
print("DataFrame:\n", df)

# Perform comparison operations
print("\nLess than 25:\n", df.lt(25))
print("\nGreater than 50:\n", df.gt(50))
print("\nEqual to 30:\n", df.eq(30))
print("\nLess than or equal to 30:\n", df.le(30))
print("\nGreater than or equal to 40:\n", df.ge(40))
print("\nNot equal to 30:\n", df.ne(30))

Output

Following is the output of the above code −

DataFrame:
     A   B
0  10  40
1  20  50
2  30  60

Less than 25:
        A      B
0   True  False
1   True  False
2  False  False

Greater than 50:
        A      B
0  False  False
1  False  False
2  False   True

Equal to 30:
        A      B
0  False  False
1  False  False
2   True  False

Less than or equal to 30:
       A      B
0  True  False
1  True  False
2  True  False

Greater than or equal to 40:
        A     B
0  False  True
1  False  True
2  False  True

Not equal to 30:
        A     B
0   True  True
1   True  True
2  False  True

Example: Binary Comparison Between Two Pandas Data Structures

This example compares the two DataFrames element-wise using the eq(), ne(), lt(), gt(), le(), and gt() functions.

import pandas as pd

# Create two DataFrames
df1 = pd.DataFrame({'A': [1, 0, 3], 'B': [9, 5, 6]})
df2 = pd.DataFrame({'A': [1, 2, 1], 'B': [6, 5, 4]})

# Display the Input DataFrames
print("DataFrame 1:\n", df1)
print("\nDataFrame 2:\n", df2)

# Perform comparison operations between two DataFrames
print("\nEqual :\n", df1.eq(df2))
print("\nNot Equal:\n", df1.ne(df2))
print("\ndf1 Less than df2:\n", df1.lt(df2))
print("\ndf1 Greater than df2:\n", df1.gt(df2))
print("\ndf1 Less than or equal to df2:\n", df1.le(df2))
print("\ndf1 Greater than or equal to df2:\n", df1.ge(df2))

Output

Following is the output of the above code −

DataFrame 1:
    A  B
0  1  9
1  0  5
2  3  6

DataFrame 2:
    A  B
0  1  6
1  2  5
2  1  4

Equal :
        A      B
0   True  False
1  False   True
2  False  False

Not Equal:
        A      B
0  False   True
1   True  False
2   True   True

df1 Less than df2:
        A      B
0  False  False
1   True  False
2  False  False

df1 Greater than df2:
        A      B
0  False   True
1  False  False
2   True   True

df1 Less than or equal to df2:
        A      B
0   True  False
1   True   True
2  False  False

df1 Greater than or equal to df2:
        A     B
0   True  True
1  False  True
2   True  True
Advertisements