pandas DataFrame Comparision method - DataFrame.gt() | pandas gt method
In this pandas tutorial we will discuss about pandas gt method used for comparison.
Introduction
DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe , The data can be a dictionary that stores list of values with specified key.
Example: Create Pandas dataframe
In this example, we will create a dataframe with 4 rows and 4 columns with integers data and assign indices through index parameter.
import pandas as pd
#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],
'column2':[100,200,300,200],
"column3":[23,45,67,43],
"column4":[1234,54,67,8]
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: DataFrame is created below
column1 column2 column3 column4
one 100 100 23 1234
two 200 200 45 54
three 300 300 67 67
four 200 200 43 8
DataFrame.gt() - greater than
This method is used to check the given value is greater than to the elements present in the dataframe or not.
If present, It will return True at that element position, otherwise it will return false.
Syntax:
dataframe_input.gt(value)
where, dataframe_input is the input pandas dataframe.
Example: pandas gt method
In this pandas gt method example, we will create a dataframe and check the gt() method functionality with two scenarios.
import pandas as pd
#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],
'column2':[100,200,300,200],
"column3":[23,45,67,43],
"column4":[1234,54,67,8]
},index=['one','two','three','four'])
#check the elements present in the dataframe are greater than 200 or not
print(data.gt(200))
print()
#check the elements present in the dataframe are greater than 23 or not
print(data.gt(23))
Output:
In the first output, the elements that are greater than 200 will be replaced by True, others are set to False.
Similarly, in the second output, the elements that are greater than 23 will be replaced by True, others are set to False.
column1 column2 column3 column4
one False False False True
two False False False False
three True True False False
four False False False False
column1 column2 column3 column4
one True True False True
two True True True True
three True True True True
four True True True False
We can also place an operator instead as gt(). The operator used is ">".
Syntax:
dataframe_input>value
Example:
In this pandas gt method example, we will create a dataframe and check the ">" operator functionality with two scenarios.
import pandas as pd
#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],
'column2':[100,200,300,200],
"column3":[23,45,67,43],
"column4":[1234,54,67,8]
},index=['one','two','three','four'])
#check the elements present in the dataframe are greater than 200 or not
print(data>200)
print()
#check the elements present in the dataframe are greater than 23 or not
print(data>23)
Output:
In the first output, the elements that are greater than 200 will be replaced by True, others are set to False.
Similarly, in the second output, the elements that are greater than 23 will be replaced by True, others are set to False.
column1 column2 column3 column4
one False False False True
two False False False False
three True True False False
four False False False False
column1 column2 column3 column4
one True True False True
two True True True True
three True True True True
four True True True False
We can also compare with column wise by providing different values.
Note - The total values must be equal to column count
Syntax:
dataframe_input.gt([values])
Example: pandas gt method
In this pandas gt method example, we will compare the dataframe with 4 values (since number of columns are 4)
import pandas as pd
#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],
'column2':[100,200,300,200],
"column3":[23,45,67,43],
"column4":[1234,54,67,8]
},index=['one','two','three','four'])
#display actual dataframe
print(data)
print()
#check the elements present in the dataframe are greater than to the given values in list or not
print(data.gt([100, 200, 23,8]))
Output:
From the above example, the first value - 100 is compared with first column and set to True whereever 100 is greater than the element present in the first column,the second value - 200 is compared with second column and set to True whereever 200 is greater than the element present in the second column,the third value - 23 is compared with third column and set to True whereever 23 is greater than the element present in the third column and the forth value - 8 is compared with forth column and set to True whereever 8 is greater than the element present in the forth column. Remaining all are set to False.
column1 column2 column3 column4
one 100 100 23 1234
two 200 200 45 54
three 300 300 67 67
four 200 200 43 8
column1 column2 column3 column4
one False False False True
two True False True True
three True True True True
four True False True False
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Apr 28,2023