Python Pandas Series ge() Function

Pandas Series ge() Function:

The ge() function of the Pandas module compares series and other, element-by-element, for greater than equal to and returns the outcome of the comparison. It is comparable to series >= other, but with the ability to use a fill_value as one of the parameters to replace missing data.

Syntax:

Series.ge(other, level=None, fill_value=None, axis=0)

Parameters

other: This is required. It indicates a Series or a scalar value.

level: This is optional. To broadcast over a level, specify an int or a name that matches the Index values on the passed MultiIndex level. The type of this may be int or name. None is the default value.

fill_value: This is optional. It indicates a value to fill in any current missing (NaN) values, as well as any new elements required for successful Series alignment. The result will be missing if data is missing in both corresponding Series locations. The type of this may be float or None. None is the default value.

axis:  This is optional. To apply the method by rows, use 0 or ‘index,’ and to apply it by columns, use 1 or ‘columns.’

Return Value:

The result of the comparison i.e, a boolean series is returned.

Pandas Series ge() Function in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above-given series.
  • Apply ge() function on all the elements of the given series by passing some random number to it and print the result.
  • Here it compares if all the elements of the given series are >= 40
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series([12, 25, 65, 40, 100])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Apply ge() function on all the elements of the given series
# by passing some random number to it and print the result.
# Here it compares if all the elements of the given series are >= 40
print("Checking if all the elements of the given series are >= 40:")
print(gvn_series.ge(40))

Output:

The given series is:
0     12
1     25
2     65
3     40
4    100
dtype: int64

Checking if all the elements of the given series are >= 40:
0    False
1    False
2     True
3     True
4     True
dtype: bool

Example2

Approach:

  • Import pandas module using the import keyword.
  • Import numpy module using the import keyword.
  • Pass some random list, index values as the arguments to the Series() function of the pandas module to create the first series and store it in a variable.
  • Pass some random list, index values as the arguments to the Series() function of the pandas module to create the second series and store it in another variable.
  • Print the given first series
  • Print the given second series
  • Pass the above second series, fill_value as the arguments to the ge() function and apply it on the first series to check for the greater than or equal to(>=)condition and print the result.
  • Here it compares the elements of the first series >= second series by replacing NaN values with 10.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import numpy module using the import keyword.
import numpy as np
# Pass some random list, index values as the arguments to the Series() function
# of the pandas module to create the  first series and store it in a variable.
fst_series = pd.Series([13, 25, np.NaN, 80, 70], 
              index=['P', 'Q', 'R', 'S', 'T'])
# Pass some random list, index values as the arguments to the Series() function
# of the pandas module to create the second series and store it in another variable.
scnd_series = pd.Series([9, 35, 15, 20,  np.NaN], 
              index=['P', 'Q', 'R', 'S', 'T'])

# Print the given first series
print("The given first series is:")
print(fst_series)
print()
# Print the given second series
print("The given second series is:")
print(scnd_series)
print()
# Pass the above second series, fill_value as the arguments to the ge() function 
# and apply it on the first series to check for the greater than or equal to(>=)
# condition and print the result.
# Here it compares if the elements of the first series >= second series by repalcing 
# NaN values with 10
print("Comparing if the elements of the first series >= second series:")
print(fst_series.ge(scnd_series, fill_value = 10))

Output:

The given first series is:
P    13.0
Q    25.0
R     NaN
S    80.0
T    70.0
dtype: float64

The given second series is:
P     9.0
Q    35.0
R    15.0
S    20.0
T     NaN
dtype: float64

Comparing if the elements of the first series >= second series:
P     True
Q    False
R    False
S     True
T     True
dtype: bool

Example3

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary) as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Compare both the columns gvn_list1 and gvn_list2 using the ge() function and store the result as a new column in the dataframe.
  • Here it checks if gvn_list1 >= gvn_list2 element-wise.
  • Print the dataframe after adding a new column(gvn_list1 >= gvn_list2).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary) as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme  = pd.DataFrame({
  "gvn_list1": [45, 24, 54, 90],
  "gvn_list2": [55, 7, 54, 44]
})
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Compare both the columns gvn_list1 and gvn_list2 using the ge() function
# and store the result as a new column in the dataframe.
# Here it checks if gvn_list1 >= gvn_list2 element-wise.
data_frme ['gvn_list1 >= gvn_list2'] = data_frme ['gvn_list1'].ge(data_frme ['gvn_list2'])
# Print the dataframe after adding a new column(gvn_list1 >= gvn_list2)
print("The dataframe after adding a new column(gvn_list1 >= gvn_list2):")
print(data_frme )

Output:

The given Dataframe:
   gvn_list1  gvn_list2
0         45         55
1         24          7
2         54         54
3         90         44

The dataframe after adding a new column(gvn_list1 >= gvn_list2):
   gvn_list1  gvn_list2  gvn_list1 >= gvn_list2
0         45         55                   False
1         24          7                    True
2         54         54                    True
3         90         44                    True

 

Leave a Comment