Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python program to compare two Pandas series
In this program, we will declare two Pandas series and compare their elements using comparison operators. Pandas provides element-wise comparison operations that return boolean Series objects.
Before we solve the problem, we need to import the Pandas library into our local IDE. This can be done by installing Pandas on our local machine. The command for installing Pandas is −
pip install pandas
Input Data
We will use two sample series for comparison ?
Series1 = [2,4,6,8,10] Series2 = [1,3,5,7,9]
Algorithm
- Step 1: Define two Pandas series using the Series() function of Pandas library
- Step 2: Compare the series using greater than, less than, and equal-to operators
- Step 3: Display the comparison results as boolean Series
Example Code
import pandas as pd
series1 = pd.Series([2,4,6,8,10])
series2 = pd.Series([1,3,5,7,9])
print("Series 1:")
print(series1)
print("\nSeries 2:")
print(series2)
print("\nGreater Than:")
print(series1 > series2)
print("\nLess Than:")
print(series1 < series2)
print("\nEquals:")
print(series1 == series2)
Series 1: 0 2 1 4 2 6 3 8 4 10 dtype: int64 Series 2: 0 1 1 3 2 5 3 7 4 9 dtype: int64 Greater Than: 0 True 1 True 2 True 3 True 4 True dtype: bool Less Than: 0 False 1 False 2 False 3 False 4 False dtype: bool Equals: 0 False 1 False 2 False 3 False 4 False dtype: bool
Other Comparison Operations
Pandas also supports other comparison operators ?
import pandas as pd
series1 = pd.Series([2,4,6,8,10])
series2 = pd.Series([1,3,5,7,9])
print("Greater Than or Equal:")
print(series1 >= series2)
print("\nLess Than or Equal:")
print(series1 <= series2)
print("\nNot Equal:")
print(series1 != series2)
Greater Than or Equal: 0 True 1 True 2 True 3 True 4 True dtype: bool Less Than or Equal: 0 False 1 False 2 False 3 False 4 False dtype: bool Not Equal: 0 True 1 True 2 True 3 True 4 True dtype: bool
Conclusion
Pandas Series comparison operations return boolean Series with element-wise comparison results. These operations are useful for data filtering and conditional operations in data analysis.
Advertisements
