Pandas 数据帧gt()
方法
原文:https://www.studytonight.com/pandas/pandas-dataframe-gt-method
我们可以在 Pandas 的数据帧中找到两个数字中最大的一个,在本教程中,我们将讨论和学习 Python PandasDataFrame.gt()
方法。此方法用于获取数据帧和其他元素的大于,在本教程中,我们将比较数据帧与标量、序列和其他数据帧。它返回 bool 的数据帧,这是比较的结果。
下图显示了DataFrame.gt()
方法的语法。
句法
DataFrame.gt(other, axis='columns', level=None)
因素
其他:可以是任何单个或多个元素的数据结构,也可以是类似列表的对象,例如标量、序列、序列或数据帧。
轴:“0”代表索引,“1”代表列,默认为列。表示是按索引轴比较还是按列轴比较。
级别:代表 int 或 label。它跨级别广播,匹配传递的多索引级别上的索引值。
示例:使用DataFrame.gt()
方法将数据帧与常数进行比较
这里,我们使用返回 bool 类型数据帧的DataFrame.gt()
方法与scalar
进行比较。
#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))
一旦我们运行该程序,我们将获得以下输出。
-数据帧为- A B C 0 200 60 150 1 500 250 1 -应用 gt 方法后- A B C 0 假假假 1 真真假
示例:使用DataFrame.gt()
方法将数据帧与系列进行比较
这里,我们使用DataFrame.gt()
方法与Series
进行比较。见下面的例子。
#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))
一旦我们运行该程序,我们将获得以下输出。
-数据帧为- A B C 0 200 60 150 1 500 250 1 -应用 gt 方法后- A B C 0 真假假假 1 真假假 2 假假假假
示例:使用DataFrame.gt()
方法将数据帧与其他数据帧进行比较
在这里,我们用DataFrame.gt()
方法与other DataFrame
进行比较。见下面的例子。
#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))
一旦我们运行该程序,我们将获得以下输出。
-第一个数据帧是- A B C 0 200 60 150 1 500 250 1 -第二个数据帧是- A B C 0 200 65 100 1 550 251 10 -应用 gt 方法后- A B C 0 假假真 1 假假假假
结论:
在本教程中,我们学习了 PandasDataFrame.gt()
方法。我们学习了这个方法的语法、参数,理解了 DataFrame.gt()
方法。