w3resource

Pandas Series: idxmax() function

Get the row label of the maximum value in Pandas series

The idxmax() function is used to get the row label of the maximum value.

If multiple values equal the maximum, the first row label with that value is returned.

Syntax:

Series.idxmax(self, axis=0, skipna=True, *args, **kwargs)
Pandas Series idxmax image

Parameters:

Name Description Type/Default Value Required / Optional
skipna Exclude NA/null values. If the entire Series is NA, the result will be NA. bool
Default Value: True
Required
axis For compatibility with DataFrame.idxmax. Redundant for application on Series. bool
Default Value: 0
Required
*args, **kwargs Additional keywords have no effect but might be accepted for compatibility with NumPy. Required

Returns: Index
Label of the maximum value.

Raises: ValueError
If the Series is empty.

Notes: This method is the Series version of ndarray.argmax. This method returns the label of the maximum, while ndarray.argmax returns the position. To get the position, use series.values.argmax().

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(data=[2, None, 5, 4, 5],
              index=['P', 'Q', 'R', 'S', 'T'])
s

Output:

P    2.0
Q    NaN
R    5.0
S    4.0
T    5.0
dtype: float64
Pandas Series idxmax image

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(data=[2, None, 5, 4, 5],
              index=['P', 'Q', 'R', 'S', 'T'])
s.idxmax()

Output:

'R'

Example - If skipna is False and there is an NA value in the data, the function returns nan:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(data=[2, None, 5, 4, 5],
              index=['P', 'Q', 'R', 'S', 'T'])
s.idxmax(skipna=False)

Output:

nan

Previous: Get the first n rows in Pandas series
Next: Row label of the minimum value in Pandas series



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-idxmax.php