Python Pandas DataFrame ge() Function

Pandas DataFrame ge() Function:

The ge() method of the Pandas module compares dataframe and other element-by-element for greater than equal to and returns the outcome of the comparison. It’s similar to dataframe >= other, but with the ability to select the comparison axis (rows or columns) and level.

Syntax:

DataFrame.ge(other, axis='columns', level=None)

Parameters

other: This is required. It indicates any single or multiple element data structure or list-like object

axis: This is optional. Choose between comparing by index (0 or ‘index’) or columns (1 or ‘columns’).

level: This is optional. To broadcast over a level, specify an int or a label that matches the Index values on the passed MultiIndex level. None is the default value.

Return Value:

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

Pandas DataFrame ge() Function in Python

Example1

Here, the DataFrame is compared to a scalar value using the ge() function.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe
  • Apply ge() function to the dataframe by passing some random number to it and print the result.
  • Here it compares if the entire dataframe has values >= 3
  • 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), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Apply ge() function to the dataframe by passing some random number
# to it and print the result.
# Here it compares if the entire dataframe has values >= 3
print("Checking if the entire dataframe has values >= 3:")
print(data_frme.ge(3))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

Checking if the entire dataframe has values >= 3:
        student_rollno  student_marks
virat            False           True
nick             False           True
jessy             True           True
sindhu            True           True

Example2

Here, the ‘other’ argument can be provided as a list to compare different columns with different scalar values

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe
  • Apply ge() function on the dataframe by passing list as an argument to it which compares the corresponding list elements with the dataframe and print the result.
  • Here it checks if the elements of the student_rollno column of the dataframe>=3 and student_marks column of the dataframe>=40.
  • 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), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 40, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Apply ge() function on the dataframe by passing list as an argument to it 
# which compares the corresponding list elements with the dataframe and print the result.
# Here it checks if the elements of the student_rollno column of the dataframe>=3.
# and student_marks column of the dataframe>=40.
print("Checking if the student_rollno column>=3 and student_marks column>=40:")
print(data_frme.ge([3, 40]))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             40
jessy                3             25
sindhu               4             90

Checking if the student_rollno column>=3 and student_marks column>=40:
        student_rollno  student_marks
virat            False           True
nick             False           True
jessy             True          False
sindhu            True           True

Example3

Here, ge() function is used on specific columns rather than the entire DataFrame.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Apply ge() function on the specified column of dataframe by passing some random number to it and print the result.
  • Here it compares if the elements of student_rollno column>=3
  • Apply ge() function on the specified columns of the dataframe by passing list as an argument to it which compares the corresponding list elements with the specified columns of the dataframe and print the result.
  • Here it compares if the elements of the student_rollno column of the dataframe>=3 and student_marks column of the dataframe>=40.
  • 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), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 40, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply ge() function on the specified column of dataframe 
# by passing some random number to it and print the result.
# Here it compares if the elements of student_rollno column>=3
print("Checking if the student_rollno column>=3")
print(data_frme["student_rollno"].ge(3))
print()
# Apply ge() function on the specified columns of the dataframe by passing list
# as an argument to it which compares the corresponding list elements with
# the specified columns of the dataframe and print the result.
# Here it compares if the elements of the student_rollno column of the dataframe>=3.
# and student_marks column of the dataframe>=40.
print("Checking if the student_rollno column>=3 and student_marks column>=40:")
print(data_frme[["student_rollno", "student_marks"]].ge([3, 40]))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             40
jessy                3             25
sindhu               4             90

Checking if the student_rollno column>=3
virat     False
nick      False
jessy      True
sindhu     True
Name: student_rollno, dtype: bool

Checking if the student_rollno column>=3 and student_marks column>=40:
        student_rollno  student_marks
virat            False           True
nick             False           True
jessy             True          False
sindhu            True           True

Example4

In a DataFrame, the ge() method can be used to compare two series/column element-by-element for greater than equal to condition.

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