Comparing two pandas series objects using lt() function - applying less than condition

Elements present in one pandas series can be compared with another pandas series to find whether the first is less than second using the lt() function or the < operator as given in the Python example.

Series Binaryoperatorfunctions Updated 26 Aug 2019 1 min read

Overview

  • “Less Than” is a comparison operation. For pandas Series objects, the “Less Than“ operation is implemented through the “<” operator as well as the function Series.lt();
  • When using the function lt(), Python developers has an option to provide replacement for any occurrence of None/NaN/missing values.

Example

The Python example below compares two pandas series objects of string literals and prints the resulting series on to the console.

python
# Example Python program to compare pandas series instance

# using lt() function i.e., applying less than < condition

import pandas as pds

# Define strings

token1 = "axybd";

token2 = "gbuio";

# Load the strings into lists making one character string literals

tokenList1 = list(token1);

tokenList2 = list(token2);

# Load the lists of characters into series objects

series1 = pds.Series(tokenList1);

series2 = pds.Series(tokenList2);

# Compare string literals of series1 with series2

series3 = series1.lt(series2);

print("pandas Series of strings 1:");

print(series1);

print("pandas Series of strings 2:");

print(series2);

print("pandas Series of strings 1 < pandas Series of strings 2");

print(series3);

Output

Output
pandas Series of strings 1:

0    a

1    x

2    y

3    b

4    d

dtype: object

pandas Series of strings 2:

0    g

1    b

2    u

3    i

4    o

dtype: object

pandas Series of strings 1 < pandas Series of strings 2

0     True

1    False

2    False

3     True

4     True

dtype: bool