Python | Pandas dataframe.equals()
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 : DataFrame
Returns: Scalar : boolean value
Example #1: Use equals()
function to find the result of comparison between two different dataframe objects.
import pandas as pd
df1 = pd.DataFrame({ "A" :[ 1 , 5 , 7 , 8 ],
"B" :[ 5 , 8 , 4 , 3 ],
"C" :[ 10 , 4 , 9 , 3 ]})
df2 = pd.DataFrame({ "A" :[ 5 , 3 , 6 , 4 ],
"B" :[ 11 , 2 , 4 , 3 ],
"C" :[ 4 , 3 , 8 , 5 ]})
df1
df2
|
Let’s find the result of comparison between both the data frames.
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.
import pandas as pd
df1 = pd.DataFrame({ "A" :[ 1 , 2 , 3 ],
"B" :[ 4 , 5 , None ],
"C" :[ 7 , 8 , 9 ]})
df2 = pd.DataFrame({ "A" :[ 1 , 2 , 3 ],
"B" :[ 4 , 5 , None ],
"C" :[ 7 , 8 , 9 ]})
df1
df2
|
Let’s perform comparison operation on both the dataframes.
Output :
The output scalar boolean value. True indicates that both the dataframes has equal values in the corresponding cells.
Last Updated :
20 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...