Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas dataframe.equals()
function is used to determine if two dataframe object in consideration are equal or not. Unlike dataframe.eq()
method, the result of the operation is a scalar boolean value indicating if the dataframe objects are equal or not.
Syntax: DataFrame.equals(other)
Parameters:
other : DataFrameReturns: Scalar : boolean value
Example #1: Use equals()
function to find the result of comparison between two different dataframe objects.
# importing pandas as pd import pandas as pd # Creating the first dataframe df1 = pd.DataFrame({ "A" :[ 1 , 5 , 7 , 8 ], "B" :[ 5 , 8 , 4 , 3 ], "C" :[ 10 , 4 , 9 , 3 ]}) # Creating the second dataframe df2 = pd.DataFrame({ "A" :[ 5 , 3 , 6 , 4 ], "B" :[ 11 , 2 , 4 , 3 ], "C" :[ 4 , 3 , 8 , 5 ]}) # Print the first dataframe df1 # Print the second dataframe df2 |
Let’s find the result of comparison between both the data frames.
# To find the comparison result df1.equals(df2) |
Output :
The output is False because the two dataframes are not equal to each other. They have different elements.
Example #2: Use equals()
function to test for equality between two data frame object with NaN
values.
Note : NaNs in the same location are considered equal.
# importing pandas as pd import pandas as pd # Creating the first dataframe df1 = pd.DataFrame({ "A" :[ 1 , 2 , 3 ], "B" :[ 4 , 5 , None ], "C" :[ 7 , 8 , 9 ]}) # Creating the second dataframe df2 = pd.DataFrame({ "A" :[ 1 , 2 , 3 ], "B" :[ 4 , 5 , None ], "C" :[ 7 , 8 , 9 ]}) # Print the first dataframe df1 # Print the second dataframe df2 |
Let’s perform comparison operation on both the dataframes.
# To find the comparison between two dataframes df1.equals(df2) |
Output :
The output scalar boolean value. True indicates that both the dataframes has equal values in the corresponding cells.