How to apply Greater Than or Equal operation on the elements of two series objects?


In the pandas series constructor, there is a method called ge() which is used to apply Greater Than or Equal to comparison operation between elements of two pandas series objects.

The output of this method is a new series object with boolean values(True Or False). If True, the element which is Greater than or Equal to the corresponding element in other series objects. It gives False for remaining values.

There are 3 parameters for this ge() method, which is fill_value, other, and level. The other parameter takes the 2nd input (series or a scalar), fill_value parameter takes None or float value, used to replace the missing values by this specified data while executing the method.

By default, this parameter will fill missing values with Nan, and the level parameter takes int or name to Broadcast across a level.

Example 1

Given below is an example to understand how to apply greater than or equal operation on the elements of two series objects in Pandas.

# importing packages
import pandas as pd
import numpy as np

# Creating Pandas Series objects
series1 = pd.Series([1, np.nan, 12, np.nan, 86, 89], index=list("ABCDEG"))
print('First series object:',series1)

series2 = pd.Series([np.nan, np.nan, 2, 74, 72, 61], index=list("ABCDEF"))
print('Second series object:',series2)

# apply ge() method
result = series1.ge(series2)
print("Output:")
print(result)

Explanation

Initially, we have created the two pandas series objects by using the pandas series constructor. Then, then applied the ge() method between them.

Output

The output is given below −

First series object:
A    1.0
B    NaN
C    12.0
D    NaN
E    86.0
G    89.0
dtype: float64

Second series object:
A    NaN
B    NaN
C    2.0
D    74.0
E    72.0
F    61.0
dtype: float64

Output:
A    False
B    False
C    True
D    False
E    True
F    False
G    False
dtype: bool

We can observe in the above output block, the method has successfully returned the result of Greater than or Equal to comparison operation of the given two series objects.

Example 2

Let’s take the previous example and this time apply the ge() method by replacing the missing values with an integer value 1.

# importing packages
import pandas as pd
import numpy as np

# Creating Pandas Series objects
series1 = pd.Series([1, 12, np.nan, 86, 89], index=list("ABCDE"))
print('First series object:',series1)

series2 = pd.Series([32, 2, 74, 72, 61], index=list("ABCDF"))
print('Second series object:',series2)

# apply ge() method
result = series1.ge(series2, fill_value=1)
print("Output:")
print(result)

Output

The output is given below −

First series object:
A 1.0
B 12.0
C NaN
D 86.0
E 89.0
dtype: float64

Second series object:
A    32
B    2
C    74
D    72
F    61
dtype: int64

Output:
A    False
B    True
C    False
D    True
E    True
F    False
dtype: bool

The pandas series.ge() method initially replaced the missing values while execution and then performed the comparison open, we can see the resultant series object in the above output block.

Updated on: 07-Mar-2022

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements