w3resource logo
Python Exercises

Python Pandas: Compare the elements of the two Pandas Series

Python Pandas: Date Series Exercise-4 with Solution

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

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

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                                             

Python Code Editor:

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)

Improve this sample solution and post your code through Disqus