Python

Pandas Argmax()

A function named argmax() is available in the constructor of Pandas to determine where the Series/DataFrame maximum value is located. The argmax() method returns an integer value which designates the location of the largest value. Let’s see the syntax for the Index, Series, and DataFrame.

Syntax:

Index –

pandas.Index.argmax(axis=None)

Series –

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

DataFrame –

pandas.DataFrame_object[‘column’].argmax()

Parameters:

  1. Where the skipna parameter excludes the NA/null values, the outcome is NA if the whole series is NA.
  2. axis: To be compatible with the DataFrame.idxmax, redundant use with the Series.
  3. Additional keywords *args and **kwargs have no impact. However, they may be accepted for NumPy compatibility.
  4. idxmax: The index of the highest value is returned.

Example 1: Index()

Create an Index that stores 7 values which includes the None/NaN values.

  1. Return the maximum value index position by ignoring the NaN values.
  2. Return the maximum value index position by considering the NaN values.
import pandas
import numpy

# Create the Index
shops=pandas.Index([10,345,67,89,90,None,numpy.nan])

print(shops,"\n")

# Return the Maximum element index position
print(shops.argmax(),"\n")

# Return the Maximum element index position by considering NaN values
print(shops.argmax(skipna=False))

Output:

Explanation:
First, we display the entire Index.

  1. In the second output, 345 is the largest value among 7 values and its index position is 1.
  2. In the last output, we consider the NaN values. Since there is a NaN value, -1 is returned.

Example 2: Series()

Create a Pandas Series named “shops” that stores 5 values which include the NaN value.

  1. Return the maximum value index position by ignoring the NaN values.
  2. Return the maximum value index position by considering the NaN values.
import pandas
import numpy

# Consider the Series data
shops=pandas.Series([100,45,67,78,numpy.nan])

print(shops,"\n")

# Return the Maximum element index position
print(shops.argmax(),"\n")

# Return the Maximum element index position by considering NaN values
print(shops.argmax(skipna=False))

Output:

Explanation:
First, we display the entire Series.

  1. In the second output, 100 is the largest value among 5 values and its index position is 0.
  2. In the last output, we consider the NaN values. Since there is a NaN value at the last position, -1 is returned.

Example 3: DataFrame()

So far, we have seen how to find the maximum value’s index position, Now, we will see how to find it in the DataFrame column. Quickly create a Pandas DataFrame named “results” which stores 4 columns and 5 rows having None/NaN values.

  1. Return the maximum value index position by ignoring the NaN values.
  2. Return the maximum value index position by considering the NaN values.
import pandas
import numpy
results = pandas.DataFrame([["Internal", 98,"pass",numpy.nan],
                   ["Internal", 45,"fail",None],
                   ["External", None,"pass",None],
                   ["External", numpy.nan,"pass",None],
                  [None, 18,"fail",90]],
                  columns=["Exam","Score","Res","Other"],
                  index = ['Ram','Sravan','Govind','Anup', 'bob']
                  )
 
 
print(results,"\n")

# Return the Maximum element index position in the "Exam" column
print(results['Other'].argmax())

# Return the Maximum element index position in the "Score" column
print(results['Score'].argmax())

Output:

Explanation:

First, we display the entire DataFrame.

  1. In the second output, 90.0 is the largest value among 5 values in the “Other” column. Its index position is 4.
  2. In the last output, 98.0 is the largest value among 5 values in the “Marks” column. Its index position is 0.

Conclusion

This article showed how to locate the index location of the maximum value (or values) in a DataFrame or Series using the Index.argmax() function, Series.argmax, and DataFrame[‘column’].argmax functions in this tutorial. Initially, we showed how to comprehend the function’s parameters before discovering how to use the argmax() function on various Python built-in functions.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain