# What is the basic operation of the series.equals() method in pandas?

PandasServer Side ProgrammingProgramming

The basic operation of the series.equals() method in the pandas constructor is used to test whether the elements in two series objects are the same or not, and it also compares the shape of the two series object.

The equals() method is very similar to the pandas series.eq() method but the difference is it will return a boolean value as a result, whereas the eq() method returns a series object with boolean values.

The output boolean value True indicates the elements in two series objects are the same. And it indicates False for unequal elements in series objects.

## Example 1

In the following example, we will see how the equals() method performs the comparison operation on elements of two series objects.

# importing pandas package
import pandas as pd

# create pandas Series1
series1 = pd.Series([34, 12, 21, 65, 89])

print("First series object:",series1)

# create pandas Series2
series2 = pd.Series([34, 21, 89, 65, 12])
print("second series object:",series2)

result = series1.equals(series2)
print("Result :", result)

## Explanation

So initially, we have created two pandas Series objects with the same elements but the positions are changed.

After successfully creating the two series objects we have applied the equals() method.

## Output

The output is given below −

First series object:
0    34
1    12
2    21
3    65
4    89
dtype: int64

Second series object:
0    34
1    21
2    89
3    65
4    12
dtype: int64

Result: False

In the above output block, we can see both input series objects series1, series2, and output of the equals() method. Here, The equals() method returns False which indicates the element in the two given series objects are not the same.

## Example 2

In the following example, we will see how the equals() method works for two series objects with the same data but the data types of index labels are different.

# importing pandas package
import pandas as pd

# create pandas Series1
series1 = pd.Series([78, 22, 12])

print("First series object:",series1)

# create pandas Series2
series2 = pd.Series([78, 22, 12], index=[0.0, 1.0, 2.0])
print("second series object:",series2)

result = series1.equals(series2)
print("Result :", result)

## Output

The output is given below −

First series object:
0    78
1    22
2    12
dtype: int64

Second series object:
0.0    78
1.0    22
2.0    12
dtype: int64

Result: True

For the equals() method the data type of elements should be the same. But the data type of index labels no need to have the same dtype. That’s why the equals() method returns True for the following example.

Updated on 07-Mar-2022 07:00:08