- 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.
- 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("Series of integers:");
print("Result of applying the greater than function(gt):");
Series of floating point numbers:
Series of integers:
Result of applying the greater than function(gt):