+

Python | Pandas Series.equals ()

Series.equals() Pandas Series.equals() checks if two objects contain the same elements ... This function allows you to compare two Series or DataFrames with each other to see if they have the same shape and elements.

Syntax: Series.equals (other)

Parameter:
other: The other Series or DataFrame to be compared with the first.

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

Example # 1: Use Series.equals () to check if the underlying data in two given series objects is the same or not.

# import pandas as pd

import pandas as pd

 
# Create the first episode

sr1 = pd.Series ([ 80 , 25 , 3 , 25 , 24 , 6 ])

 
# Create the second episode

sr2 = pd.Series ([ 80 , 25 , 3 , 80 , 24 , 25 ])

  
# Create index

index_ = [ `Coca Cola` , `Sprite` , ` Coke` , ` Fanta` , `Dew` , `ThumbsUp` ]

 
# set the index of the first episode

sr1. index = index_

  
# set the second series index

sr2.index = i ndex_

 
# Print first episode

print (sr1)

 
# Print the second series

print (sr2 )

Output:

We will now use Series.equals () to check if the underlying data in two given series objects is the same or not.

# test for equality

result = sr1.equals (other = sr2)

 
# Print result

print (result)

Output:


As we see in the output , Series.equals () returned False indicating that the element in the two given series objects does not match.

Example # 2: Use Series.equals () to check if the underlying data in two given series objects is the same or not.

# import pandas as pd

import pandas a s pd

 
# Create the first episode

sr1 = pd.Series ([ 80 , 25 , 3 , 25 , 24 , 6 ])

 
# Create the second episode

sr2 = pd.Series ([ 80 , 25 , 3 , 25 , 24 , 6 ])

 
# Create index

index_ = [ `Coca Cola` , ` Sprite` , `Coke` , ` Fanta` , `Dew` , ` ThumbsUp` ]

 
# set first series index

sr1.index = index_

 
# set the second series index

sr2.index = index_

 
# Print first series

print (sr1)

 
# Print the second batch

print (sr2)

Output:

Now we will use Series.equals () to check if the underlying data in two given series objects is the same or not.

# equality test

result = sr1.equals (other = sr2)

 
# Print result

print (result)

Output:

As we can see in the output, Series.equals () returned True indicating that the element in the two given series objects is the same.

Get Solution for free from DataCamp guru