pandas.DataFrame.idxmax#

DataFrame.idxmax(axis=0, skipna=True, numeric_only=False)[源码]#

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

NA/null 值将被排除。

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

要使用的轴。0 或 ‘index’ 表示逐行,1 或 ‘columns’ 表示逐列。

skipnabool, 默认 True

排除 NA/null 值。如果整个 DataFrame 为 NA,或者如果 skipna=False 且存在 NA 值,则此方法将引发 ValueError

numeric_onlybool, default False

仅包含 floatintboolean 数据。

返回:
Series

沿指定轴的最大值的索引。

引发:
ValueError
  • 如果行/列为空

另请参阅

Series.idxmax

返回最大元素的索引。

注意

此方法是 ndarray.argmax 的 DataFrame 版本。

示例

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

>>> 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: str

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

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