Pandas 数据帧idxmax()方法

原文:https://www.studytonight.com/pandas/pandas-dataframe-idxmax-method

在本教程中,我们将讨论和学习 PandasDataFrame.idxmax()的方法。该方法用于获取请求轴上最大值的第一次出现的索引。如果有任何空值或缺失值,将被排除。此方法返回一个序列,该序列由沿指定轴的最大值索引组成。如果数据帧中的行或列为空,它会引发值错误

下面是DataFrame.idxmax()方法的语法。

句法

DataFrame.idxmax(axis=0, skipna=True)

因素

轴:“0”【指数】代表行方向,【1】【列】代表列方向。它表示使用哪个轴。

skipna: 代表 bool(真或假),默认值为 True。当此参数为真时,它排除所有空值或缺失值。如果数据帧中的整行或整列为空值,结果也将为空。

示例:使用DataFrame.idxmax()方法获取索引

考虑一个数据集,使用DataFrame.idxmax()方法获取每列中最大值的索引。见下面的例子。

#importing pandas as pd
import pandas as pd
df = pd.DataFrame({'Marks_1': [85,90,45],'Marks_2': [85,96,100]},index=['Kannada', 'English', 'Science'])
print("-----The DataFrame is----")
print(df)
print("----- Index of the maximum value over the row axis----")
print(df.idxmax())

一旦我们运行代码,我们将获得以下输出。

-数据帧为- Marks_1 Marks_2 卡纳达语 85 85 英语 90 96 科学 45 100 -行轴上最大值的索引- Marks_1 英语 Marks_2 科学 数据类型:对象

示例:使用DataFrame.idxmax()方法获取索引

考虑一个数据集,使用DataFrame.idxmax()方法获取每行中最大值的索引。见下面的例子。

#importing pandas as pd
import pandas as pd
df = pd.DataFrame({'Marks_1': [85,90,45],'Marks_2': [95,46,100]},index=['Kannada', 'English', 'Science'])
print("-----The DataFrame is----")
print(df)
print("----- Index of the maximum value over the column axis----")
print(df.idxmax(axis="columns"))

一旦我们运行代码,我们将获得以下输出。

-数据帧为- Marks_1 Marks_2 卡纳达语 85 95 英语 90 46 科学 45 100 -列轴上最大值的索引- 卡纳达语 Marks_2 英语 Marks_1 科学 Marks_2 数据类型:对象

示例:使用DataFrame.idxmax()方法获取索引

该数据集由空值组成,我们试图使用DataFrame.idxmax()方法获取每行中最大值的索引。

#importing pandas as pd
import pandas as pd
df = pd.DataFrame({'Marks_1': [85,None,45],'Marks_2': [None,46,None]},index=['Kannada', 'English', 'Science'])
print("-----The DataFrame is----")
print(df)
print("----- Index of the maximum value over the column axis----")
print(df.idxmax(axis="columns"))

一旦我们运行代码,我们将获得以下输出。

-数据帧为- Marks_1 Marks_2 卡纳达 85.0 NaN 英语 NaN 46.0 科学 45.0 NaN -列轴上最大值的索引- 卡纳达 Marks_1 英语 Marks_2 科学 Marks_1 dtype: object

结论:

在本教程中,我们学习了 PandasDataFrame.idxmax()方法。我们学习了该方法的语法,并将其应用于数据帧,以理解 DataFrame.idxmax()方法。