w3resource

Pandas Series: equals() function

Test Pandas objects contain the same elements

The equals() function is used to test whether two Pandas objects contain the same elements.

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. The column headers do not need to have the same type, but the elements within the columns must be the same dtype.

Syntax:

Series.equals(self, other)
Pandas Series equals image

Parameters:

Name Description Type/Default Value Required / Optional
other The other Series or DataFrame to be compared with the first. Series or DataFrame Required

Returns: bool
True if all elements are the same in both objects, False otherwise.

Example :

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({2: [20], 3: [30]})
df

Output:

   2	3
0	20	30

Example - DataFrames df and exactly_equal have the same types and values for their elements and column labels, which will return True:

Python-Pandas Code:

import numpy as np
import pandas as pd
exactly_equal = pd.DataFrame({2: [20], 3: [30]})
exactly_equal

Output:

   2	3
0	20	30

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({2: [20], 3: [30]})
exactly_equal = pd.DataFrame({2: [20], 3: [30]})
df.equals(exactly_equal)

Output:

True

Example - DataFrames df and different_column_type have the same element types and values, but have different types for the column labels, which will still return True:

Python-Pandas Code:

import numpy as np
import pandas as pd
different_column_type = pd.DataFrame({2.0: [20], 3.0: [30]})
different_column_type

Output:

   2.0	3.0
0	20	30

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({2: [20], 3: [30]})
different_column_type = pd.DataFrame({2.0: [20], 3.0: [30]})
df.equals(different_column_type)

Output:

True

Example - DataFrames df and different_data_type have different types for the same values for their elements, and will return False even though their column labels are the same values and types:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({2: [20], 3: [30]})
different_data_type = pd.DataFrame({2: [20.0], 3: [30.0]})
df.equals(different_data_type)

Output:

False

Previous: Indicate duplicate Series values
Next: Subset initial periods of Pandas time series



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-equals.php