Compare Two Pandas Series Objects Applying Greater Than Condition - Gt() Function

Overview:

  • Elements of one pandas Series object can be compared with the corresponding elements of another pandas Series object, and checked whether the first element is greater than the second.
  • The results are returned as a separate pandas Series, consisting of test results as Boolean values - True and False.

Example:

  • The following Python program uses numpy to generate a sample of ten random floating-point numbers and a sample of ten integers.
  • These samples(ndarray objects) are loaded into pandas Series objects and checked whether the elements from the floating-point Series is greater than the elements from the integer Series. The resultant Series with Boolean values is printed onto the console.

# Example program to check whether each element of a pandas Series

# is greater than the corresponding element from another pandas Series

import pandas as pds

import numpy as np

 

# Generate random floating point numbers

fractions = 4 * np.random.random_sample(10);

 

# Generate random integers

integers  = np.random.randint(10, size=10);

 

# Load the floats into a pandas Series

series1 = pds.Series(fractions);

series2 = pds.Series(integers);

 

# Check whether each element of series1 is greater than

# the corresponding element from series2

series3 = series1.gt(series2);

 

print("Series of floating point numbers:");

print(series1);

 

print("Series of integers:");

print(series2);

 

print("Result of applying the greater than function(gt):");

print(series3);

 

Output:

Series of floating point numbers:

0    0.617934

1    3.462905

2    0.283336

3    1.708910

4    0.741405

5    1.742751

6    3.657854

7    0.137984

8    3.203777

9    3.666683

dtype: float64

Series of integers:

0    8

1    6

2    3

3    4

4    9

5    3

6    2

7    0

8    1

9    7

dtype: int64

Result of applying the greater than function(gt):

0    False

1    False

2    False

3    False

4    False

5    False

6     True

7     True

8     True

9    False

dtype: bool


Copyright 2023 © pythontic.com