Often you may want to find the index of the first occurrence of a minimum value in a pandas DataFrame along a particular axis.

The most efficient way to do so is by using the **idxmin()** function, which uses the following syntax:

**DataFrame.idxmin(axis=0, skipna=True, numeric_only=False)**

where:

**axis**: The axis to use (0=row-wise, 1=column-wise)**skipna**: Whether to exclude NA values or not. Default is True.**numeric_only**: Whether to include only float, int or boolean data.

The following example shows how to use the **idxmin()** function in practice with a pandas DataFrame.

**Example: How to Use the idxmin() Function in Pandas**

Suppose we create the following pandas DataFrame that contains information about various basketball players:

import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'C', 'C', 'C'], 'points': [12, 18, 18, 22, 30, 41, 12], 'assists': [8, 10, 11, 11, 7, 12, 8]}) #view DataFrame print(df) team points assists 0 A 12 8 1 A 18 10 2 B 18 11 3 B 22 11 4 C 30 7 5 C 41 12 6 C 12 8

Suppose that we would like to use the **idxmin()** function to find the index of the minimum value in each column.

Suppose that we attempt to use the following syntax:

#find index of minimum value in each column df.idxmin() TypeError: reduction operation 'argmin' not allowed for this dtype

We receive a **TypeError** because the first column named team is a string column and pandas is unable to find the “minimum” value of a string.

Instead, we must use the following syntax to find the index of the minimum value of only the **points** and **assists** columns:

#find index of minimum value in points and assists columns df[['points', 'assists']].idxmin() points 0 assists 4 dtype: int64

The output displays the index of the minimum value in only the **points** and **assists** columns.

For example, we can see:

- The minimum value in the
**points**column occurs in index position**0**(i.e. the first value in the column). - The minimum value in the
**assists**column occurs in index position**4**.

We can view the original DataFrame to confirm that the minimum value in the **points** column is **12**, which first occurs in index position **0** of that column.

We can also confirm that the minimum value in the **assists** column is **7**, which first occurs in index position **4** of that column.

By default, the **idxmin()** function finds the index of the minimum value of each column.

However, we can specify **axis=1** to instead find the index of the minimum value of each row.

For example, we can use the following syntax to do so:

#find index of minimum value in each row df[['points', 'assists']].idxmin(axis=1) 0 assists 1 assists 2 assists 3 assists 4 assists 5 assists 6 assists dtype: object

The output displays **assists** for each row because the value in the assists column happens to be smaller than the value in the points column for each row in the DataFrame.

**Note**: You can find the complete documentation for the **idxmin()** function in pandas here.

**Additional Resources**

The following tutorials explain how to perform other common tasks in pandas:

How to Use the Rolling.apply() Function in Pandas

How to Use the nunique() Function in Pandas

How to Use the get_loc() Function in Pandas

How to Create a Tuple from Two Columns in Pandas