Pandas DataFrame gt() Method
We can find the greatest of two numbers in the panda's DataFrame and in this tutorial, we will discuss and learn the Python pandas DataFrame.gt()
method. This method is used to get greater than of dataframe and other, element-wise and in this tutorial, we will compare the DataFrame with the scalar, Series, and other DataFrame. It returns the DataFrame of bool that is the result of the comparison.
The below shows the syntax of the DataFrame.gt()
method.
Syntax
DataFrame.gt(other, axis='columns', level=None)
Parameters
other: It can be any single or multiple element data structure, or list-like object, for example, scalar, sequence, Series, or DataFrame.
axis:'0' represents the index and '1' represents the columns and the default is columns. It represents whether to compare by the index axis or column axis.
level: It represents the int or label. It broadcasts across a level, matching Index values on the passed MultiIndex level.
Example: Comparing the DataFrame with the constant using the DataFrame.gt()
Method
Here, we are comparing with a scalar
using the DataFrame.gt()
method that returns a bool-type dataframe.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df=pd.DataFrame({"A":[200,500],"B":[60,250],"C":[150,1]})
print("--------The DataFrame is---------")
print(df)
print("----After applying gt function-----")
print(df.gt(200))
Once we run the program we will get the following output.
--------The DataFrame is---------
A B C
0 200 60 150
1 500 250 1
----After applying gt method-----
A B C
0 False False False
1 True True False
Example: Comparing the DataFrame with the Series using the DataFrame.gt()
Method
Here, we are comparing with a Series
using the DataFrame.gt()
method. See the below example.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df=pd.DataFrame({"A":[200,500],"B":[60,250],"C":[150,1]})
print("--------The DataFrame is---------")
print(df)
series = pd.Series([150, 200,150])
print("----After applying gt function-----")
print(df.gt(series,axis=0))
Once we run the program we will get the following output.
--------The DataFrame is---------
A B C
0 200 60 150
1 500 250 1
----After applying gt method-----
A B C
0 True False False
1 True True False
2 False False False
Example: Comparing the DataFrame with the other DataFrame using the DataFrame.gt()
Method
Here, we are comparing with other DataFrame
using the DataFrame.gt()
method. See the below example.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df_1=pd.DataFrame({"A":[200,500],"B":[60,250],"C":[150,1]})
print("--------The first DataFrame is---------")
print(df_1)
df_2=pd.DataFrame({"A":[200,550],"B":[65,251],"C":[100,10]})
print("--------The second DataFrame is---------")
print(df_2)
print("----After applying gt function-----")
print(df_1.gt(df_2))
Once we run the program we will get the following output.
--------The first DataFrame is---------
A B C
0 200 60 150
1 500 250 1
--------The second DataFrame is---------
A B C
0 200 65 100
1 550 251 10
----After applying gt method-----
A B C
0 False False True
1 False False False
Conclusion:
In this tutorial, we learned the Python pandas DataFrame.gt()
method. We learned the syntax, parameters of this method and understood the DataFrame.gt()
method.