
To check whether two pandas dataframe or series is equal, we use the DataFrame.equals() method. This function allows two Series or DataFrames to be compared against each other to see if they have the same shape and elements. NaNs in the same location are considered equal.
Syntax –
DataFrame.equals(other)
other – The other Series or DataFrame to be compared with the first.
Examples –
Let’s create a dataframes.
import pandas as pd
df = pd.DataFrame({'A':[1, 2, 3, 4, 5],
'B':[6, 7, 8, 9, 10],
'C':[11, 12, 13, 14, 15]})
df

Now, create a dataframe that is exactly equal to the first dataframe.
exactly_equal = pd.DataFrame({'A':[1, 2, 3, 4, 5],
'B':[6, 7, 8, 9, 10],
'C':[11, 12, 13, 14, 15]})
exactly_equal

Now, if we compare these two dataframes then the equals() method should return True.
df.equals(exactly_equal)
#output
True
Now, Let’s create a dataframe that is different than the first dataframe
different_df = pd.DataFrame({'A': [16, 17, 18, 19, 20],
'B':[21, 22, 23, 24, 25],
'C':[26, 27, 28, 29, 10]})
different_df

Now, if we compare these two dataframes, the equals() method should return False.
df.equals(different_df)
#output
False