Comparing values from two pandas series objects using le() function - applying less than equal to condition

Overview:

  • Using le() function of Series class, it can be determined that whether the values present in a pandas Series is less than or equal to the values present in an another pandas Series.
  • The Series.le() function returns a Boolean value for each comparison of the elements from the two Series instances.
  • The le() function is a variant for the less than or equal to operator (“<=”). The le() function is convenient when replacing any NaN/None values present in the Series instances.

 

Example:

# Example Python program to compare two pandas Series instances using le() function

import pandas as pds

 

# Marks obtained in Semester 1

series1 = pds.Series([70, 67, 75, 80, 85, 90]);

 

# Marks obtained in Semester 2

series2 = pds.Series([74, 71, 69, 70, 85, 94]);

 

# Compare whether marks obtained in second semester is less than or equal to

# the marks obtained in the first semester

lessThanResults = series1.le(series2);

print("Contents of series1:");

print(series1);

 

print("Contents of series2:")

print(series2);

 

print("Result of comparing series1 and series2 using le():");

print(lessThanResults);

 

 

Output:

Contents of series1:

0    70

1    67

2    75

3    80

4    85

5    90

dtype: int64

Contents of series2:

0    74

1    71

2    69

3    70

4    85

5    94

dtype: int64

Result of comparing series1 and series2 using le():

0     True

1     True

2    False

3    False

4     True

5     True

dtype: bool


Copyright 2024 © pythontic.com