Pandas Tutorial Pandas Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Pandas Series - gt() function



The Pandas gt() function is used to get greater than of series and argument, element-wise (binary operator gt). It is equivalent to series > other, but with support to substitute a fill_value for missing data as one of the parameters. The syntax for using this function is given below:

Syntax

Series.gt(other, level=None, fill_value=None)

Parameters

other Required. Specify scalar value or Series.
level Optional. Specify int or name to broadcast across a level, matching Index values on the passed MultiIndex level. Default is None.
fill_value Optional. Specify value to fill existing missing (NaN) values, and any new element needed for successful Series alignment. If data in both corresponding Series locations is missing the result will be missing. Default is None.

Return Value

Returns the result of the comparison.

Example: Comparing Series with a scalar value

In the example below, the gt() function is used to get the result of comparing for greater than of the Series with given scalar value.

import pandas as pd
import numpy as np

x = pd.Series([10, 20, 30, 40, 50])

print("The Series contains:")
print(x)

#comparing Series > 25
print("\nx.gt(25) returns:")
print(x.gt(25))

The output of the above code will be:

The Series contains:
0    10
1    20
2    30
3    40
4    50
dtype: int64

x.gt(25) returns:
0    False
1    False
2     True
3     True
4     True
dtype: bool

Example: Comparing Series with a Series

A series can be compared with another series element-wise for greater than of. Consider the following example:

import pandas as pd
import numpy as np

x = pd.Series([10, np.NaN, 30, 40, 50], index=['A', 'B', 'C', 'D', 'E'])
y = pd.Series([5, 20, 35, np.NaN, 65], index=['A', 'B', 'C', 'D', 'E'])

print("The x contains:")
print(x)

print("\nThe y contains:")
print(y)

#calculating x > y
print("\nx.gt(y, fill_value=0) returns:")
print(x.gt(y, fill_value=0))

The output of the above code will be:

The x contains:
A    10.0
B     NaN
C    30.0
D    40.0
E    50.0
dtype: float64

The y contains:
A     5.0
B    20.0
C    35.0
D     NaN
E    65.0
dtype: float64

x.gt(y, fill_value=0) returns:
A     True
B    False
C    False
D     True
E    False
dtype: bool

Example: Using gt() on columns of a DataDrame

The gt() function can be applied in a data frame to get the result of comparing for greater than of two series/column element-wise. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "col1": [10, 20, 30, 40, 50],
  "col2": [5, 15, 30, 45, 55]
})

print("The DataFrame is:")
print(df)

#calculating 'col1' > 'col2'
df['Result'] = df['col1'].gt(df['col2'])

print("\nThe DataFrame is:")
print(df)

The output of the above code will be:

The DataFrame is:
   col1  col2
0    10     5
1    20    15
2    30    30
3    40    45
4    50    55

The DataFrame is:
   col1  col2  Result
0    10     5    True
1    20    15    True
2    30    30   False
3    40    45   False
4    50    55   False

❮ Pandas Series - Functions

5