Linux Hint Posted January 30, 2023 Share Posted January 30, 2023 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: Where the skipna parameter excludes the NA/null values, the outcome is NA if the whole series is NA. axis: To be compatible with the DataFrame.idxmax, redundant use with the Series. Additional keywords *args and **kwargs have no impact. However, they may be accepted for NumPy compatibility. 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. Return the maximum value index position by ignoring the NaN values. 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. In the second output, 345 is the largest value among 7 values and its index position is 1. 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. Return the maximum value index position by ignoring the NaN values. 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. In the second output, 100 is the largest value among 5 values and its index position is 0. 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. Return the maximum value index position by ignoring the NaN values. 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. In the second output, 90.0 is the largest value among 5 values in the “Other” column. Its index position is 4. 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. View the full article Quote Link to comment Share on other sites More sharing options...

## Recommended Posts

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.