*22*

You can use the **pandas.DataFrame.idxmax()** function to return the index of the maximum value across a specified axis in a pandas DataFrame.

This function uses the following syntax:

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

where:

**axis**: The axis to use (0 = rows, 1 = columns). Default is 0.**skipna**: Whether or not to exclude NA or null values. Default is True.

The following examples show how to use this function in practice with the following pandas DataFrame:

import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 8, 9, 23], 'assists': [5, 7, 7, 9, 12, 9], 'rebounds': [11, 8, 11, 6, 6, 5]}, index=['Andy','Bob', 'Chad', 'Dan', 'Eric', 'Frank']) #view DataFrame df points assists rebounds Andy 25 5 11 Bob 12 7 8 Chad 15 7 11 Dan 8 9 6 Eric 9 12 6 Frank 23 9 5

**Example 1: Find Index that has Max Value for Each Column**

The following code shows how to find the index that has the maximum value for each column:

#find index that has max value for each column df.idxmax(axis=0) points Andy assists Eric rebounds Andy dtype: object

From the output we can see:

- The player with the highest value in the
**points**column is Andy. - The player with the highest value in the
**assists**column is Eric. - The player with the highest value in the
**rebounds**column is Andy.

It’s important to note that the idxmax() function will return the **first occurrence** of the maximum value.

For example, notice that Andy and Chad both had 11 rebounds. Since Andy appears first in the DataFrame his name is returned.

**Example 2: Find Column that has Max Value for Each Row**

The following code shows how to find the column that has the maximum value for each row:

#find column that has max value for each row df.idxmax(axis=1) Andy points Bob points Chad points Dan assists Eric assists Frank points dtype: object

From the output we can see:

- The highest value in the row labelled “Andy” can be found in the
**points**column. - The highest value in the row labelled “Bob” can be found in the
**points**column. - The highest value in the row labelled “Chad” can be found in the
**points**column. - The highest value in the row labelled “Dan” can be found in the
**assists**column. - The highest value in the row labelled “Eric” can be found in the
**assists**column. - The highest value in the row labelled “Andy” can be found in the
**points**column.

Refer to the pandas documentation for a complete explanation of the idxmax() function.

**Additional Resources**

How to Find the Max Value of Columns in Pandas

How to Calculate the Mean of Columns in Pandas

How to Calculate the Sum of Columns in Pandas