How to compare elements of a series by Python list using pandas series.ge() function?


By using the series.ge() method we can apply the Greater Than or Equal to comparison operation on elements of the series with a python list. The functionality of this ge() method in pandas series class is to check Greater Than or Equal to compare operation between elements of a series with another.

Here another is one of the parameters of this ge() method, by using this we can give our second input (series or a scalar) and also we can apply this ge() Greater Than or Equal to comparison operation between series and a python list.

Example 1

In the following example, we will see how the ge() method compares elements of a series with elements of a python list.

import pandas as pd
import numpy as np

# create pandas Series
s = pd.Series([49, 96, 38, 13, 22])

print("Series object:",s)

# apply ge() method using a list of integers
print("Output:")
print(s.ge(other=[1, 92, 30, 29, 59]))

Moreover, we can see how the series.ge() method works on elements of the series and python list.

Output

The output is given below −

Series object:
0    49
1    96
2    38
3    13
4    22
dtype: int64

Output:
0    True
1    True
2    True
3    False
4    False
dtype: bool

The first element in the series object 49 is compared with the first element of the list 1 (49 >= 1) then the corresponding output will be True which is represented in the resultant series object. In the same way, the remaining elements are also compared.

Example 2

Let’s take another pandas series object and apply the Greater Than or Equal to comparison operation with a list of integers.

import pandas as pd
import numpy as np

# create pandas Series
s = pd.Series({'A':6, 'B':18, "C":None, "D":43, 'E':np.nan, 'F':30 })

print("Series object:",s)

# apply ge() method using a list of integers by replacing missing values
print("Output:")
print(s.ge(other=[1, 19, 30, 29, 88, 59], fill_value=30))

Explanation

Also, we have replaced the missing values by specifying integer value 30 to the fill_value parameter.

Output

The output is as follows −

Series object:
A    6.0
B    18.0
C    NaN
D    43.0
E    NaN
F    30.0
dtype: float64

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

The ge() method successfully replaced the missing values by 30 and then compared the elements of two input objects.

Updated on: 07-Mar-2022

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements