How to check if the elements of a series object are Greater Than or Equal to scalar value?

PandasServer Side ProgrammingProgramming

The series.ge() method in the pandas constructor is used to apply the Greater Than or Equal to comparison operation between elements of the given series with another (maybe another series or a scalar value). The comparison operation is exactly equivalent to series >= Other.

To check the Greater Than or Equal comparison operation between elements of the given series with scalar, we will use this series.ge() method. Here we need to send the scalar value as a parameter to the series.ge() method. Then the method will compare the elements of the series object with the specified scalar value.

As a result, the method will return a new series object with boolean values. It returns True for every element which is Greater than or Equal to the scalar. Otherwise, it will return False.

Example 1

In this following example, we have applied the Greater than or Equal to comparison operation between a series object and scalar.

# importing packages
import pandas as pd
import numpy as np

#create pandas series
sr = pd.Series([53, 28, np.nan, 87, 72, 21, np.nan, 12])
print(sr)

# apply ge() method with a scalar value
result = sr.ge(28)
print(result)

Output

The output is given below −

0    53.0
1    28.0
2    NaN
3    87.0
4    72.0
5    21.0
6    NaN
7    12.0
dtype: float64

0    True
1    True
2    False
3    True
4    True
5    False
6    False
7    False
dtype: bool

The first element in the series object 53.0 is compared with the scalar 28 which is nothing but 53.0 >= 28 then the expected output will be True which is represented in the corresponding index of the output series object. In the same way, the remaining elements of the series object are compared with 28.

Example 2

In the previous example, the null values are also compared with the scalar value and the result will be False because Nan == anything will be False. So this time, we are replacing those missing values with an integer value 50 by using the fill_value parameter.

# importing packages
import pandas as pd
import numpy as np

#create pandas series
sr = pd.Series([46, np.nan, 35, 59, 87, 72, np.nan, 12])
print(sr)

# apply ge() method using a scalar value by replacing missing values
result = sr.ge(50, fill_value=50)
print("Output:")
print(result)

Output

The output is as follows −

0    46.0
1    NaN
2    35.0
3    59.0
4    87.0
5    72.0
6    NaN
7    12.0
dtype: float64

Output:
0    False
1    True
2    False
3    True
4    True
5    True
6    True
7    False
dtype: bool

In the above output block, We can observe that the Nan values are replaced with “50”.

raja
Updated on 07-Mar-2022 10:52:36

Advertisements