Python | Pandas Series.gt ()



Pandas Series.gt() is used to compare two series and return a boolean value for each matching item.

Syntax: Series.gt (other, level = None, fill_value = None, axis = 0)

Parameters:
other: other series to be compared with
level: int or name of level in case of multi level
fill_value: Value to be replaced instead of NaN
axis: 0 or `index` to apply method by rows and 1 or `columns` to apply by columns.

Return type: Boolean series

Note: results are returned based on the calling series comparison & gt; another series.

To download the dataset used in the following example, click here.

In the following examples, the data frame used contains data for some NBA players. An image of the data frame before any operations is attached below.

Example # 1 :
In this example, the Age and Weight columns are copied using the .gt () method. Since the values ​​in the weight columns are very large compared to the age column, therefore, the values ​​are divisible by 10 first. The .dropna () method removes null rows before comparison to avoid errors.

# pandas module import

import pandas as pd 

 
# regex module import

import re

  
# create data frame

data = pd.read_csv ( " htt ps: //media.python.engineering/wp-content/uploads/nba.csv "

 
# remove null values ​​to avoid errors

data.dropna (inplace = True

 
# other series

other = data [ " Weight " ] / 10

  
# method call and return to new column

data [ "Age & gt; Weight " ] = data [ "Age" ]. gt (other)

Output:
As shown in the output image, the new column is True, where the value in the Age column is greater than Weight / 10.

Example # 2: Handling NaN Values ​​

In this example, two series are created using pd.Series () . The series also contains a zero value and therefore 5 is passed to the fill_value parameter to replace the zero values ​​with 5.

# pandas module import

import pandas as pd 

  # numpy module import

import numpy as np

 
# create episode 1

series1 = pd.Series ([ 24 , 19 , 2 , 33 , 49 , 7 , np.nan, 10 , np.nan])

 
# create series 2

series2 = pd.Series ([ 16 , np.nan, 2 , 23 , 5 , 40 , np.nan, 0 , 9 ])

 
# setting the replacement value to zero

na_replace = 5

 
# call and save the result

result = series1.gt (series2, fill_value = na_replace)

 
# display

result

Output:
As you can see from the results, the NaN values ​​have been replaced with 5, and the comparison is done after the replacement, and the new values ​​are used for comparison .

 0 True 1 True 2 False 3 True 4 True 5 False 6 False 7 True 8 False dtype: bool