Sometimes we need to compare pandas series to perform some comparative analysis. It is possible to compare two pandas Series with help of Relational operators, we can easily compare the corresponding elements of two series at a time. The result will be displayed in form of True or False. And we can also use a function like Pandas Series.equals() to compare two pandas series.
Method 1: Using Relational operator
Example 1: Checking if two Series elements are equal or not
Python3
import pandas as pd
ps1 = pd.Series([ 2.5 , 4 , 6 , 8 , 10 , 1.75 , 40 ])
ps2 = pd.Series([ 1.5 , 3 , 5 , 7 , 10 , 1.75 , 20 ])
print ( "Series1:" )
print (ps1)
print ( "\nSeries2:" )
print (ps2)
print ( "\nCompare the elements of the two given Series:" )
print ( "\nEqual:" )
print (ps1 = = ps2)
print ( "\nNot Equal:" )
print (ps1 ! = ps2)
|
Output:

In the above example, we compare the elements of two series ‘ps1‘ and ‘ps2‘ to check whether they are equal or not.
Example 2: Checking if series 1 elements are greater than series 2
Python3
import pandas as pd
ps1 = pd.Series([ 2.5 , 4 , 6 , 8 , 10 , 1.75 , 40 ])
ps2 = pd.Series([ 1.5 , 3 , 5 , 7 , 10 , 1.75 , 20 ])
print ( "Series1:" )
print (ps1)
print ( "\nSeries2:" )
print (ps2)
print ( "\nCompare the elements of the two given Series:" )
print ( "\nGreater than:" )
print (ps1 > ps2)
|
Output:

In the above example, we compare the elements of two series ‘ps1‘ and ‘ps2‘ to check if elements of ps1 are greater than that of ps2.
Example 3: Checking if series 1 elements are less than series 2
Python3
import pandas as pd
ps1 = pd.Series([ 2.5 , 4 , 6 , 8 , 10 , 1.75 , 40 ])
ps2 = pd.Series([ 1.5 , 3 , 5 , 7 , 10 , 1.75 , 20 ])
print ( "Series1:" )
print (ps1)
print ( "\nSeries2:" )
print (ps2)
print ( "\nCompare the elements of the two given Series:" )
print ( "\nLess than:" )
print (ps1 < ps2)
|
Output:

In the above example, we compare the elements of two series ‘ps1‘ and ‘ps2‘ to check if elements of ps1 are less than that of ps2.
Method 2: Using Pandas Series.equals() function
Pandas Series.equals() function test whether two 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.
Syntax:
Series.equals(other)
Example:
Python3
import pandas as pd
ps1 = pd.Series([ 2.5 , 4 , 6 , 8 , 10 , 1.75 , 40 ])
ps2 = pd.Series([ 1.5 , 3 , 5 , 7 , 10 , 1.75 , 20 ])
print ( "Series1:" )
print (ps1)
print ( "\nSeries2:" )
print (ps2)
print ( "\nResult of comparing Two Series:" )
result = ps1.equals(other = ps2)
print (result)
|
Output:

In the above example, we compare given two pandas series ‘ps1′ and ‘ps2’ using function Series.equals().
Example 2:
Python3
import pandas as pd
ps1 = pd.Series([ 80 , 25 , 3 , 25 , 24 , 6 ])
ps2 = pd.Series([ 80 , 25 , 3 , 25 , 24 , 6 ])
print ( "Series1:" )
print (ps1)
print ( "\nSeries2:" )
print (ps2)
print ( "\nResult of comparing Two Series:" )
result = ps1.equals(other = ps2)
print (result)
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Oct, 2020
Like Article
Save Article