Python

Pandas argmax( )

A function named argmax() is available in the constructor of pandas to determine where the series’ maximum value is located. The pandas series is a row index-valued, uni-dimensional object of data structure. To determine the index position of the series object’s maximum value, use the argmax() method of the pandas’ library. The argmax() method returns an integer value designating the location of the largest value. It is an operation that identifies the argument in a target function yielding the highest value. The most typical application of argmax in machine learning is to identify the class with the highest predicted probability although argmax can be built manually.

How To Use pandas argmax() Function in Windows

The largest value existing in the input index is returned as the indices by the pandas Index.argmax() function. It yields the index of the first instance of the maximum value if there are multiple maximum values (i.e., the greatest value is existing more than once).

Syntax:

Index.argmax(axis=None)

Parameter: Doesn’t take any parameter.

The row label for the highest value in the specified series object is returned by the pandas Series.argmax() function.

Syntax:

series.argmax(axes=0, skipna=True, *args, **kwargs)

Where the skipna parameter excludes NA/null values, the outcome will be NA if the whole series is NA.

axis: To be compatible with DataFrame.idxmax. Redundant for use with Series.

Additional keywords *args and **kwargs have no impact, however, may be accepted for NumPy compatibility.

idxmax: The index of the highest value, is returned.

The following are some examples in which we will see the use of pandas argmax() function:

Example 1: Find the Index With Maximum Value Using index.argmax() Function

First, we will import the pandas library as “pd”. Python is the foundation for the open-source pandas data analysis library. Python is instructed to import the data analysis library of pandas into the current system or environment by the import pandas section of the code. Following that, the as pd section of the code instructs Python to assign pandas the abbreviation for “pd”. After importing pandas libraries we will create a data frame consisting of some numerical values.

Each value in this column is stored by Python as a 64-bit integer, as indicated by the type int64. To find the index with the maximum, we will use an in-built function of Python, which is the argmax() function.

The index of arrays and data frame starts from 0 so you can see index 2 having the greatest value, which is 99. We cannot use argmax() function for a data frame consisting of string or character data types.

As you can see, the dtype = ‘object’. If we attempt this, it will show an error on the console.

It can only find the greatest numerical value from the dataset.

Example 2: Find the Index With Maximum Value When It is Occurring More Than One Using an index.argmax() Function

Similar to the previous example, we will import pandas as “pd”. Then, we will create a data frame consisting of multiple numeric values.

In the previous data frame, 76 is the greatest value, but it is occurring more than once. Do two indices come out of it?

The result shows that the largest number was returned as the first instance by the Index.argmax() function.

Example 3: Using the index.argmax() Function With Floating Value Data Frame

Let’s create a data frame consisting of floating values.

As you can see, dtype=’float64’ is indicating that each value in this data frame is a floating value and is stored in python as a 64-bit floating value. In this data frame, the maximum value is 9.1 and is located at index 4.

Example 4: To Retrieve the Row Label of the Highest Value in the Specified Series Using the Series.argmax() Function

First, we will create a pd.series after importing the pandas. A pandas series is a one-dimensional, labeled data structure that may hold Python objects and other data types, like texts and numbers. It is the main data structure used by pandas to store one-dimensional data and is constructed on top of the numpy array. The pandas series can be generated by utilizing the constructor pandas in Python. After creating the series, we will specify names for the indexes of the data frame.

We have specified the names of each index of corresponding values. To return the row label for the highest value in the specified series object, we will now utilize the Series.argmax() function.

The row label for the largest value in the specified series object was correctly returned by the Series.argmax() function, as seen in the output.

Let’s try another example with date_range() function. To obtain a DatetimeIndex with a fixed frequency, use the date_range() function. The initial date and the total days after will be passed to the date_range() function of pandas (known as periods). To generate the date in this format YY-MM-DD, we have also used the datetime library in this instance.

To return the row label for the highest value in the specified series object, we will now utilize the Series.argmax() function.

Since 2026-12-31 02:30:00 has the maximum value so by using the argmax() function, we will get index 4.

Example 5: Find the Location of a Maximum Value Containing Null Values

In the previous examples, we have seen the series and data frames with repeating and non-repeating values. Now, let’s create a series of elements with some null values. Along with pandas, we will also import numpy in this example. One of the most frequently used Python scientific computing packages is Numpy. It offers an object that is a multidimensional array as well as variants like masks and matrices that can be applied to different mathematical processes.

In this illustration, the series elements have null values by default. The argmax() method only takes into account the integer elements while ignoring null values to determine the position of the larger value.

Example 6: Apply the argmax() Function on a Series With Random Values
We will create a series with random values. For this, we will use the np.random.randint() function. One of the functions in Numpy for performing random sampling is randint(). It gives back a specified-shaped array that is filled with random integers in the range [low, high], or from low to high inclusively. We will build a pandas series object with eight randomly chosen integer values in the range of 1 to 50 and then use the argmax() function to determine where the maximum value falls within the range of values in the series.

In the previous example, the integer value “4” returned by the argmax() function of the pandas series object denotes the position of the greatest value of the specified series element.

Conclusion

This article showed how to locate the index location of the maximum value (or values) in a data frame or series using the index.argmax() function and series.agrmax function in this tutorial. Initially, we taught how to comprehend the function’s parameters before discovering how to use the argmax() function on various Python built-in functions. We implemented multiple examples to teach how to use the argmax() function in different conditions using different functions.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.