Pandas DataFrame eq() Function:
Python print entire dataframe: The eq() function of the Pandas module compares dataframe and other element-by-element for equality(equal to)and returns the outcome of the comparison. It’s similar to dataframe == other, but with the ability to select an axis (rows or columns) and a comparison level.
Syntax:
DataFrame.eq(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:
Pandas eq: The result of the comparison i.e, a boolean series is returned.
- Python Pandas Series eq() Function
- Python Pandas Series ge() Function
- Python Pandas Series ne() Function
Python Pandas DataFrame eq() Function
Example1
Here, the DataFrame is compared to a scalar value using the eq() 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 eq() 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, 3, 25, 90]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Apply eq() 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.eq(3))
Output:
The given Dataframe: student_rollno student_marks virat 1 80 nick 2 3 jessy 3 25 sindhu 4 90 Checking if the entire dataframe has values == 3: student_rollno student_marks virat False False nick False True jessy True False sindhu False False
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 eq() 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, 40]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Apply eq() 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.eq([3, 40]))
Output:
The given Dataframe: student_rollno student_marks virat 1 80 nick 2 40 jessy 3 25 sindhu 4 40 Checking if the student_rollno column==3 and student_marks column==40: student_rollno student_marks virat False False nick False True jessy True False sindhu False True
Example3
Here, eq() 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 eq() 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 eq() 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, 40]}, index= ["virat", "nick" , "jessy", "sindhu"] ) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Apply eq() 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 has values ==3") print(data_frme["student_rollno"].eq(3)) print() # Apply eq() 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"]].eq([3, 40]))
Output:
The given Dataframe: student_rollno student_marks virat 1 80 nick 2 40 jessy 3 25 sindhu 4 40 Checking if the student_rollno column has values ==3 virat False nick False jessy True sindhu False Name: student_rollno, dtype: bool Checking if the student_rollno column==3 and student_marks column==40: student_rollno student_marks virat False False nick False True jessy True False sindhu False True
Example4
In a DataFrame, the eq() method can be used to compare two series/column element-by-element for 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 eq() 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, 7, 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 eq() 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'].eq(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 7 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 7 7 True 2 54 54 True 3 90 44 False