w3resource

Pandas Data Series: Compare the elements of the two Pandas Series


4. Series Comparison

Write a Pandas program to compare the elements of the two Pandas Series.

Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 10]

Sample Solution:

Python Code :

import pandas as pd
ds1 = pd.Series([2, 4, 6, 8, 10])
ds2 = pd.Series([1, 3, 5, 7, 10])
print("Series1:")
print(ds1)
print("Series2:")
print(ds2)
print("Compare the elements of the said Series:")
print("Equals:")
print(ds1 == ds2)
print("Greater than:")
print(ds1 > ds2)
print("Less than:")
print(ds1 < ds2)

Sample Output:

Series1:                                                               
0     2                                                                
1     4                                                                
2     6                                                                
3     8                                                                
4    10                                                                
dtype: int64                                                           
Series2:                                                               
0     1                                                                
1     3                                                                
2     5                                                                
3     7                                                                
4    10                                                                
dtype: int64                                                           
Compare the elements of the said Series:                               
Equals:                                                                
0    False                                                             
1    False                                                             
2    False
3    False                                                             
4     True                                                             
dtype: bool                                                            
Greater than:                                                          
0     True                                                             
1     True                                                             
2     True                                                             
3     True                                                             
4    False                                                             
dtype: bool                                                            
Less than:                                                             
0    False                                                             
1    False                                                             
2    False                                                             
3    False                                                             
4    False                                                             
dtype: bool                                             

Explanation:

The above exercise demonstrates how to perform element-wise comparison operations between two Pandas Series objects using Python's comparison operators.

Following code creates two Pandas Series objects named 'ds1' and 'ds2' containing a sequence of five integers each:

ds1 = [2, 4, 6, 8, 10]
ds2 = [1, 3, 5, 7, 10]

print(ds1 == ds2): This line compares the two Series objects using the '==' operator, resulting in a new Series object with boolean values indicating whether the corresponding elements in ‘ds1’ and ‘ds2’ are equal or not. The output will be: [False, False, False, False, True]

print(ds1 > ds2): This line compares the two Series objects using the '>' operator, resulting in a new Series object with boolean values indicating whether the corresponding elements in ‘ds1’ are greater than those in ‘ds2’ or not. The output will be: [True, True, True, True, False]

print(ds1 < ds2): This line compares the two Series objects using the '<' operator, resulting in a new Series object with boolean values indicating whether the corresponding elements in ‘ds1’ are less than those in ‘ds2’ or not. The output will be: [False, False, False, False, False]


For more Practice: Solve these Related Problems:

  • Write a Pandas program to compare two Series and return a Series of booleans indicating if elements are within a tolerance of 5%.
  • Write a Pandas program to compare two Series element-wise and identify indices where the absolute difference exceeds a threshold.
  • Write a Pandas program to compare two Series and return a Series with 'Equal' or 'Not Equal' based on element matching.
  • Write a Pandas program to compare two Series of strings and return the indices where strings differ in length.

Go to:


Previous: Write a Pandas program to add, subtract, multiple and divide two Pandas Series.
Next: Write a Pandas program to convert a dictionary to a Pandas series.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.