How does the pandas series.ge() method works if the series object contains string type elements?


The series.ge() method in the pandas constructor performs the Greater Than or Equal To comparison operation between the elements of a series object with another (maybe another series or a scalar value). And this comparison operation is exactly equal to “series >= Other”.

Here we will see how the series.ge() method performs the Greater Than or Equal to comparison operation on two input objects if their elements have string type data.

If the series contains some string values then in that case the comparison is done with their ASCII values. And we only compare a string element with a corresponding element with the same data type. Otherwise, it will raise the TypeError. We cannot compare a string element of one series with an integer element of another input.

Example 1

In this following example, two series are created using the pandas.Series() constructor with a list of integers and strings.

# importing packages
import pandas as pd
import numpy as np

# Creating Pandas Series objects
series1 = pd.Series(['a', 72, np.nan, 'e', 'C', 78, 'D', 7])
print('First series object:',series1)

series2 = pd.Series(['A', 69, 87, 'E', 'C', 162, 'd', 2])
print('Second series object:',series2)

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

Output

The output is given below −

First series object:
0    a
1    72
2    NaN
3    e
4    C
5    78
6    D
7    7
dtype: object

Second series object:
0    A
1    69
2    87
3    E
4    C
5    162
6    d
7    2
dtype: object

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

The series.ge() method successfully applied the Greater Than or Equal to comparison operation between the elements of two series objects, but here both the series objects contain some string elements in this case the comparison is done with their ASCII values.

Example 2

In this example, we have replaced the missing values (Nan) by specifying an integer value (100) to the fill_value parameter.

# importing packages
import pandas as pd
import numpy as np

# Creating Pandas Series objects
series1 = pd.Series(['a', 72, np.nan, 'e', 'C', 78, 'D', 7])
print('First series object:',series1)

series2 = pd.Series(['A', 69, 87, 'E', 'C', 162, 'd', 2])
print('Second series object:',series2)

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

Output

The output is mentioned below −

First series object:
0    a
1    72
2    NaN
3    e
4    C
5    78
6    D
7    7
dtype: object

Second series object:
0    A
1    69
2    87
3    E
4    C
5    162
6    d
7    2
dtype: object

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

The ge() method successfully compares the elements of two series objects and it does not generate any errors because we have compared the same data type elements.

Updated on: 07-Mar-2022

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements