Greater Than Equal to comparison for the elements from two pandas Series using ge() function

Overview:

  • The function ge() compares elements from one Series with elements from another Series and determines whether the first element is greater than or equal to the second element. The results of the comaprisions are returned as a Series.
  • Calling the method ge() is equal to applying the >= operator . However, calling the function ge() allows a fill_value parameter to be specified for replacing the NaN/None values with default values.
  • In the same way elements of a pandas Series can be compared with any Python sequence such as list and tuple.

Example - Comparing elements from two series objects using ge():

# Python example program to compare pandas series objects

import pandas as pds

 

# Compare strings present in two pandas.series objects

series1 = pds.Series(["Hello", "World"]);

series2 = pds.Series(["Hello", "Universe"]);

 

comparisionResult = series1.ge(series2);

print("Result of comparing two pandas series objects with strings using ge():");

print(comparisionResult);

 

# Compare integers present in two pandas.series objects

series1 = pds.Series([2, 4, 6, 8 , None]);

series2 = pds.Series([1, 3, 6, 9, 10]);

 

print("Result of comparing two pandas series objects with integers using ge():");

comparisionResult = series1.ge(series2, fill_value=0);

 

print(comparisionResult);

 

 

Output:

Result of comparing two pandas series objects with strings using ge():

0    True

1    True

dtype: bool

Result of comparing two pandas series objects with integers using ge():

0     True

1     True

2     True

3    False

4    False

dtype: bool

 

Example – Comparing a pandas Series with a Python sequence using ge():

# Python example program to compare a pandas series

# with a Python sequence

import pandas as pds

 

s1 = pds.Series(["a", "b", "c", "d", "e"]);

s2 = ("c", "a", "d", "b", "e");

 

resultsSeries = s1.ge(s2);

 

print("Contents of the pandas series instance s1:");

print(s1);

print("Contents of the pandas series instance s2:");

print(s2);

 

print("Result of comparing the elements of a series with a Python sequence using ge():");

print(resultsSeries);

 

 

Output:

Contents of the pandas series instance s1:

0    a

1    b

2    c

3    d

4    e

dtype: object

Contents of the pandas series instance s2:

('c', 'a', 'd', 'b', 'e')

Result of comparing the elements of a series with a Python sequence using ge():

0    False

1     True

2    False

3     True

4     True

dtype: bool

 


Copyright 2024 © pythontic.com