🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.idxmax.html
pandas.Series.idxmax — pandas 3.0.1 documentation - PyData |
Series.idxmax(axis=0, skipna=True, *args, **kwargs)[source]# Return the row label of the maximum value. If multiple values equal the maximum, the first row label with that value is returned. Parameters: axis{0 or ‘index’} Unused. Parameter needed for compatibility with DataFrame.
🌐
W3Schools
w3schools.com › python › pandas › ref_df_idxmax.asp
Pandas DataFrame idxmax() Method
The idxmax() method returns a Series with the index of the maximum value for each column. By specifying the column axis (axis='columns'), the idxmax() method returns a Series with the index of the maximum value for each row.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.idxmax.html
pandas.DataFrame.idxmax — pandas 3.0.2 documentation
Series.idxmax · Return index of the maximum element. Notes · This method is the DataFrame version of ndarray.argmax. Examples · Consider a dataset containing food consumption in Argentina. >>> df = pd.DataFrame( ... { ... "consumption": [10.51, 103.11, 55.48], ...
🌐
w3resource
w3resource.com › pandas › series › series-idxmax.php
Pandas Series: idxmax() function - w3resource
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. ... Returns: Index Label of the maximum value. Raises: ValueError If the Series is empty.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.idxmax.html
pandas.Series.idxmax — pandas 2.3.3 documentation
Series.idxmax(axis=0, skipna=True, *args, **kwargs)[source]# Return the row label of the maximum value. If multiple values equal the maximum, the first row label with that value is returned. Parameters: axis{0 or ‘index’} Unused. Parameter needed for compatibility with DataFrame.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.25.1 › reference › api › pandas.Series.idxmax.html
pandas.Series.idxmax — pandas 0.25.1 documentation
>>> s = pd.Series(data=[1, None, 4, 3, 4], ... index=['A', 'B', 'C', 'D', 'E']) >>> s A 1.0 B NaN C 4.0 D 3.0 E 4.0 dtype: float64 · >>> s.idxmax() 'C' If skipna is False and there is an NA value in the data, the function returns nan. >>> s.idxmax(skipna=False) nan · index · modules | next | previous | pandas 0.25.1 documentation » ·
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 1.5 › reference › api › pandas.Series.idxmax.html
pandas.Series.idxmax — pandas 1.5.2 documentation
Series.idxmax(axis=0, skipna=True, *args, **kwargs)[source]# Return the row label of the maximum value. If multiple values equal the maximum, the first row label with that value is returned. Parameters · axis{0 or ‘index’} Unused. Parameter needed for compatibility with DataFrame.
🌐
TutorialsPoint
tutorialspoint.com › how-does-the-pandas-series-idxmax-method-work
How does the pandas Series idxmax() method work?
The idxmax() method of the pandas series constructor is used to get the index label of maximum value over the series data. As we know, the pandas series is a single-dimensional data structure object with axis labels. And we can access the la
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › python-pandas-series-idxmax
Python | Pandas Series.idxmax() - GeeksforGeeks
February 13, 2019 - ... # importing pandas as pd import ... series print(sr) Output : Now we will use Series.idxmax() function to find the index label corresponding to the maximum value in the series....
Find elsewhere
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.17.0 › generated › pandas.DataFrame.idxmax.html
pandas.DataFrame.idxmax — pandas 0.17.0 documentation
Series.idxmax · Notes · This method is the DataFrame version of ndarray.argmax. index · modules | next | previous | pandas 0.17.0 documentation » · API Reference » · © Copyright 2008-2014, the pandas development team.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.idxmax.html
pandas.DataFrame.idxmax — pandas 3.0.1 documentation
Series.idxmax · Return index of the maximum element. Notes · This method is the DataFrame version of ndarray.argmax. Examples · Consider a dataset containing food consumption in Argentina. >>> df = pd.DataFrame( ... { ... "consumption": [10.51, 103.11, 55.48], ...
🌐
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.idxmax.html
pandas.DataFrame.idxmax — pandas documentation
Series.idxmax · Return index of the maximum element. Notes · This method is the DataFrame version of ndarray.argmax. Examples · Consider a dataset containing food consumption in Argentina. >>> df = pd.DataFrame( ... { ... "consumption": [10.51, 103.11, 55.48], ...
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.25.0 › reference › api › pandas.Series.idxmax.html
pandas.Series.idxmax — pandas 0.25.0 documentation
>>> s = pd.Series(data=[1, None, 4, 3, 4], ... index=['A', 'B', 'C', 'D', 'E']) >>> s A 1.0 B NaN C 4.0 D 3.0 E 4.0 dtype: float64 · >>> s.idxmax() 'C' If skipna is False and there is an NA value in the data, the function returns nan. >>> s.idxmax(skipna=False) nan · index · modules | next | previous | pandas 0.25.0 documentation » ·
Top answer
1 of 2
2

What about a custom function? Something like

import numpy as np
import pandas as pd

s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])

def Max_Argmax(series):  # takes as input your series
   values = series.values  # store numeric values
   indexes = series.index  # store indexes
   Argmax = np.argmax(values)  # save index of max
   return values[Argmax], indexes[Argmax] # return max and corresponding index

(max, index) = Max_Argmax(s)

I run it on my PC and I get:

>>> s
a   -1.854440
b    0.302282
c   -0.630175
d   -1.012799
e    0.239437
dtype: float64

>>> max
0.3022819091746019

>>> index
'b'

Hope it helps!

2 of 2
2

As Jon Clements mentioned:

In [3]: s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
In [4]: x, y = s.agg(['max', 'idxmax'])
In [5]: x
Out[5]: 1.6339096862287581
In [6]: y
Out[6]: 'b'
In [7]: s
Out[7]: a    1.245039
        b    1.633910
        c    0.619384
        d    0.369604
        e    1.009942
        dtype: float64

In response to asking for a tuple:

def max_and_index(series):
    """Return a tuple of (max, idxmax) from a pandas.Series"""
    x, y = series.agg(['max', 'idxmax'])
    return x, y

t = max_and_idxmax(s)
print(t)
(1.6339096862287581, 'b')
print(type(t))
<class 'tuple'>

Even smaller:

def max_and_idxmax(series):
    """Return a tuple of (max, idxmax) from a pandas.Series"""
    return series.max(), series.idxmax()

If you need speed, use the numpy method above

import pandas as pd
import numpy as np


def max_and_index(series):
    x, y = series.agg(['max', 'idxmax'])
    return x, y

def max_and_idxmax(series):
    return series.max(), series.idxmax()

def np_max_and_argmax(series):
    return np.max(series.values), np.argmax(series.values)

def Max_Argmax(series):
   v = series.values
   i = series.index
   arg = np.argmax(v)
   return v[arg], i[arg]


a = []
for i in range(2,9,1):
    a.append(pd.Series(np.random.randint(0, 100, size=10**i)))
    print('{}\t{:>11,}'.format(i-2, 10**i))

# 0            100
# 1          1,000
# 2         10,000
# 3        100,000
# 4      1,000,000
# 5     10,000,000
# 6    100,000,000

idx = 5
%%timeit -n 2 -r 10
max_and_index(a[idx])
# 144 ms ± 5.45 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)

%%timeit -n 2 -r 10
max_and_idxmax(a[idx])
# 143 ms ± 5.14 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)

%%timeit -n 2 -r 10
Max_Argmax(a[idx])
# 9.89 ms ± 1.13 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)

%%timeit -n 2 -r 10
np_max_and_argmax(a[idx])
# 24.5 ms ± 1.74 ms per loop (mean ± std. dev. of 10 runs, 2 loops each)
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.1.2 › reference › api › pandas.Series.idxmax.html
pandas.Series.idxmax — pandas 2.1.2 documentation - PyData |
Series.idxmax(axis=0, skipna=True, *args, **kwargs)[source]# Return the row label of the maximum value. If multiple values equal the maximum, the first row label with that value is returned. Parameters: axis{0 or ‘index’} Unused. Parameter needed for compatibility with DataFrame.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.SeriesGroupBy.idxmax.html
pandas.core.groupby.SeriesGroupBy.idxmax — pandas 2.3.3 documentation
SeriesGroupBy.idxmax(axis=<no_default>, skipna=True)[source]# Return the row label of the maximum value. If multiple values equal the maximum, the first row label with that value is returned. Parameters: axis{0 or ‘index’} Unused. Parameter needed for compatibility with DataFrame.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.2.1 › reference › api › pandas.Series.idxmax.html
pandas.Series.idxmax — pandas 2.2.1 documentation - PyData |
Series.idxmax(axis=0, skipna=True, *args, **kwargs)[source]# Return the row label of the maximum value. If multiple values equal the maximum, the first row label with that value is returned. Parameters: axis{0 or ‘index’} Unused. Parameter needed for compatibility with DataFrame.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.1.1 › reference › api › pandas.Series.idxmax.html
pandas.Series.idxmax — pandas 2.1.1 documentation - PyData |
Series.idxmax(axis=0, skipna=True, *args, **kwargs)[source]# Return the row label of the maximum value. If multiple values equal the maximum, the first row label with that value is returned. Parameters: axis{0 or ‘index’} Unused. Parameter needed for compatibility with DataFrame.