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) 

Following is the output of the above code −

 Input DataFrame: 
AB
014
156
232
389
Less than 5:
AB
0TrueTrue
1FalseFalse
2TrueTrue
3FalseFalse
Greater than 5:
AB
0FalseFalse
1FalseTrue
2FalseFalse
3TrueTrue
Less than or equal to 5:
AB
0TrueTrue
1TrueFalse
2TrueTrue
3FalseFalse
Greater than or equal to 5:
AB
0FalseFalse
1TrueTrue
2FalseFalse
3TrueTrue
Equal to 5:
AB
0FalseFalse
1TrueFalse
2FalseFalse
3FalseFalse
Not equal to 5:
AB
0TrueTrue
1FalseTrue
2TrueTrue
3TrueTrue

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.NoFunctionDescription
1lt(other[, axis, level])Element-wise less than comparison.
2gt(other[, axis, level])Element-wise greater than comparison.
3le(other[, axis, level])Element-wise less than or equal comparison.
4ge(other[, axis, level])Element-wise greater than or equal comparison.
5ne(other[, axis, level])Element-wise not equal comparison.
6eq(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)) 

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)) 

Following is the output of the above code −

 DataFrame: 
AB
01040
12050
23060
Less than 25:
AB
0TrueFalse
1TrueFalse
2FalseFalse
Greater than 50:
AB
0FalseFalse
1FalseFalse
2FalseTrue
Equal to 30:
AB
0FalseFalse
1FalseFalse
2TrueFalse
Less than or equal to 30:
AB
0TrueFalse
1TrueFalse
2TrueFalse
Greater than or equal to 40:
AB
0FalseTrue
1FalseTrue
2FalseTrue
Not equal to 30:
AB
0TrueTrue
1TrueTrue
2FalseTrue

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)) 

Following is the output of the above code −

 DataFrame 1: 
AB
019
105
236
DataFrame 2:
AB
016
125
214
Equal :
AB
0TrueFalse
1FalseTrue
2FalseFalse
Not Equal:
AB
0FalseTrue
1TrueFalse
2TrueTrue
df1 Less than df2:
AB
0FalseFalse
1TrueFalse
2FalseFalse
df1 Greater than df2:
AB
0FalseTrue
1FalseFalse
2TrueTrue
df1 Less than or equal to df2:
AB
0TrueFalse
1TrueTrue
2FalseFalse
df1 Greater than or equal to df2:
AB
0TrueTrue
1FalseTrue
2TrueTrue
Advertisements
close