pandas.core.groupby.DataFrameGroupBy.idxmax#

DataFrameGroupBy.idxmax(axis=0, skipna=True)[源代码]#

返回在请求的轴上第一次出现最大值的索引。

不包括NA/NULL值。

参数
axis{0或‘index’,1或‘Columns’},默认为0

要使用的轴。行方式为0或‘index’,列方式为1或‘Columns’。

skipna布尔值,默认为True

排除NA/NULL值。如果整行/列为NA,则结果将为NA。

退货
系列

沿指定轴的最大值索引。

加薪
ValueError
  • 如果行/列为空

参见

Series.idxmax

返回最大元素的索引。

注意事项

此方法是的DataFrame版本 ndarray.argmax

示例

考虑一个包含阿根廷食物消费的数据集。

>>> df = pd.DataFrame({'consumption': [10.51, 103.11, 55.48],
...                    'co2_emissions': [37.2, 19.66, 1712]},
...                    index=['Pork', 'Wheat Products', 'Beef'])
>>> df
                consumption  co2_emissions
Pork                  10.51         37.20
Wheat Products       103.11         19.66
Beef                  55.48       1712.00

默认情况下,它返回每列中最大值的索引。

>>> df.idxmax()
consumption     Wheat Products
co2_emissions             Beef
dtype: object

若要返回每行中最大值的索引,请使用 axis="columns"

>>> df.idxmax(axis="columns")
Pork              co2_emissions
Wheat Products     consumption
Beef              co2_emissions
dtype: object