pandas.core.groupby.DataFrameGroupBy.idxmax#

DataFrameGroupBy.idxmax(skipna=True, numeric_only=False)[source]#

Return index of first occurrence of maximum in each group.

Parameters:
skipnabool, default True

Exclude NA values.

numeric_onlybool, default False

Include only float, int or boolean data.

Added in version 1.5.0.

Returns:
DataFrame

Indexes of maxima in each column according to the group.

Raises:
ValueError
  • If a column is empty or skipna=False and any value is NA.

See also

Series.idxmax

Return index of the maximum element.

DataFrame.idxmax

Indexes of maxima along the specified axis.

Notes

This method is the DataFrame version of ndarray.argmax.

Examples

Consider a dataset containing food consumption in Argentina.

>>> df = pd.DataFrame(
...     {
...         "consumption": [10.51, 103.11, 55.48],
...         "co2_emissions": [37.2, 19.66, 1712],
...         "food_type": ["meat", "plant", "meat"],
...     },
...     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

By default, it returns the index for the maximum value in each column according to the group.

>>> df.groupby("food_type").idxmax()
                consumption   co2_emissions
food_type
animal                 Beef            Beef
plant        Wheat Products  Wheat Products