- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compare two Pandas Series Objects by Applying Greater Than Condition using gt() Function?
In the pandas series constructor, there is a method called gt() which is used to apply the Greater Than condition between elements of two pandas series objects.
The result of the gt() method is based on the comparison between elements of two series objects. The operation is equal to “element of called_series > element of passed_series”.
The resultant series object is filled with the boolean values(True Or False). True value indicates the element of called_series is Greater Than the element of passed_series. Revere for False.
Example 1
Given below is an example to compare two Pandas series objects by applying Greater Than condition using the gt() function:
# importing packages import pandas as pd import numpy as np # Creating Pandas Series objects series1 = pd.Series([34, np.nan, 82], index=['x', 'y', 'z']) print('First series object:',series1) series2 = pd.Series([np.nan, 12, 76], index=['x', 'y', 'z']) print('Second series object:',series2) # apply gt() method result = series1.gt(series2) print("Output:") print(result)
Explanation
Initially, we have created the two pandas series objects using a list of integers, and the elements of the series contain some Nan values.
Output
The output is as follows −
First series object: x 34.0 y NaN z 82.0 dtype: float64 Second series object: x NaN y 12.0 z 76.0 dtype: float64 Output: x False y False z True dtype: bool
We have applied the gt() method to compare the elements of two series objects, and we haven’t replaced the missing values, which is why we got False for x, y labeled elements of the output series.
Example 2
In the following example, we have applied the gt() method by replacing the missing values with an integer value 10.
# importing packages import pandas as pd import numpy as np # Creating Pandas Series objects series1 = pd.Series([100, 12, np.nan, 86], index=list("ABCD")) print('First series object:',series1) series2 = pd.Series([32, np.nan, 74, 61], index=list("ABCD")) print('Second series object:',series2) # apply gt() method result = series1.gt(series2, fill_value=10) print("Output:") print(result)
Output
The output is as follows −
First series object: A 100.0 B 12.0 C NaN D 86.0 dtype: float64 Second series object: A 32.0 B NaN C 74.0 D 61.0 dtype: float64 Output: A True B True C False D True dtype: bool
The gt() method replaces the missing values initially while executing the function and then performs the comparison operation.
- Related Articles
- How to compare elements of a series by Python list using pandas series.ge() function?
- How to compare elements of a series by Python list using pandas series.gt() function?
- How to add two pandas Series objects by handling None values?
- How to apply Greater Than or Equal operation on the elements of two series objects?
- Python program to compare two Pandas series
- How to find the dot product of two pandas series objects?
- How to compare two JavaScript array objects using jQuery/JavaScript?
- How to access Pandas Series elements by using indexing?
- How to compare two JavaScript Date Objects?
- How to compare two objects in JavaScript?
- MongoDB query condition to compare two fields?
- MySQL GROUP BY with WHERE clause and condition count greater than 1?
- How to compare dates if greater than another date in Excel?
- State one condition necessary to liquefy gases (other than applying high pressure).
- Java Program to Compare Two Objects
