Pandas Data Series: Check the equality of two given series
Pandas: Data Series Exercise-38 with Solution
Write a Pandas program to check the equality of two given series.
Sample Solution :
Python Code :
import pandas as pd
nums1 = pd.Series([1, 8, 7, 5, 6, 5, 3, 4, 7, 1])
nums2 = pd.Series([1, 8, 7, 5, 6, 5, 3, 4, 7, 1])
print("Original Series:")
print(nums1)
print(nums2)
print("Check 2 series are equal or not?")
print(nums1 == nums2)
Sample Output:
Original Series: 0 1 1 8 2 7 3 5 4 6 5 5 6 3 7 4 8 7 9 1 dtype: int64 0 1 1 8 2 7 3 5 4 6 5 5 6 3 7 4 8 7 9 1 dtype: int64 Check 2 series are equal or not? 0 True 1 True 2 True 3 True 4 True 5 True 6 True 7 True 8 True 9 True dtype: bool
Explanation:
nums1 = pd.Series([1, 8, 7, 5, 6, 5, 3, 4, 7, 1]) nums2 = pd.Series([1, 8, 7, 5, 6, 5, 3, 4, 7, 1])
The above code creates two Pandas series objects nums1 and nums2 containing the same set of integers.
nums1 == nums2: Here the == operator is used to compare each element of the two series and returns a new series of boolean values indicating whether each element is equal or not. The resulting series will contain True for elements that are equal in both series and False for elements that are not equal in both series.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to stack two given series vertically and horizontally.
Next: Write a Pandas program to find the index of the first occurrence of the smallest and largest value of a given series.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/pandas/python-pandas-data-series-exercise-38.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics