Use argmax() idxmax() to get the index of the max value. Then you can use loc

df.loc[df['favcount'].idxmax(), 'sn']

Edit: argmax() is now deprecated, switching for idxmax()

Answer from Steven G on Stack Overflow
🌐
Python.org
discuss.python.org › python help
Find the max or min index/column for the overall pandas dataframe - Python Help - Discussions on Python.org
April 3, 2024 - Hi, There are functions that returns the index of the max/min in a pandas column, or the column of the max/min in a pandas row, like “idxmax” or “idxmin” https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.idxmax.html ...
🌐
W3Schools
w3schools.com › python › pandas › ref_df_idxmax.asp
Pandas DataFrame idxmax() Method
By specifying the column axis (axis='columns'), the idxmax() method returns a Series with the index of the maximum value for each row.
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-the-index-of-maximum-value-in-dataframe-column
Get the index of maximum value in DataFrame column - GeeksforGeeks
September 25, 2025 - In this article, we will learn how to get maximum value in a Pandas Dataframe. To download the dataframe used in this example, click here. ... We can verify whether the maximum value is present in index or not.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.max.html
pandas.DataFrame.max — pandas 3.0.2 documentation
If you want the index of the maximum, use idxmax. This is the equivalent of the numpy.ndarray method argmax. ... Axis for the function to be applied on. For Series this parameter is unused and defaults to 0. For DataFrames, specifying axis=None will apply the aggregation across both axes.
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-maximum-values-position-in-columns-and-rows-of-a-dataframe-in-pandas
Find maximum values & position in columns and rows of a Dataframe in Pandas - GeeksforGeeks
July 15, 2025 - In this article, we are going to discuss how to find the maximum value and its index position in columns and rows of a Dataframe. ... import numpy as np import pandas as pd # List of Tuples matrix = [(10, 56, 17), (np.NaN, 23, 11), (49, 36, 55), (75, np.NaN, 34), (89, 21, 44) ] # Create a DataFrame abc = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) # output abc
Top answer
1 of 15
388

Use the pandas idxmax function. It's straightforward:

>>> import pandas
>>> import numpy as np
>>> df = pandas.DataFrame(np.random.randn(5,3),columns=['A','B','C'])
>>> df
          A         B         C
0  1.232853 -1.979459 -0.573626
1  0.140767  0.394940  1.068890
2  0.742023  1.343977 -0.579745
3  2.125299 -0.649328 -0.211692
4 -0.187253  1.908618 -1.862934
>>> df['A'].idxmax()
3
>>> df['B'].idxmax()
4
>>> df['C'].idxmax()
1
  • Alternatively you could also use numpy.argmax, such as numpy.argmax(df['A']) -- it provides the same thing, and appears at least as fast as idxmax in cursory observations.

  • idxmax() returns indices labels, not integers.

  • Example': if you have string values as your index labels, like rows 'a' through 'e', you might want to know that the max occurs in row 4 (not row 'd').

  • if you want the integer position of that label within the Index you have to get it manually (which can be tricky now that duplicate row labels are allowed).


HISTORICAL NOTES:

  • idxmax() used to be called argmax() prior to 0.11
  • argmax was deprecated prior to 1.0.0 and removed entirely in 1.0.0
  • back as of Pandas 0.16, argmax used to exist and perform the same function (though appeared to run more slowly than idxmax).
  • argmax function returned the integer position within the index of the row location of the maximum element.
  • pandas moved to using row labels instead of integer indices. Positional integer indices used to be very common, more common than labels, especially in applications where duplicate row labels are common.

For example, consider this toy DataFrame with a duplicate row label:

In [19]: dfrm
Out[19]: 
          A         B         C
a  0.143693  0.653810  0.586007
b  0.623582  0.312903  0.919076
c  0.165438  0.889809  0.000967
d  0.308245  0.787776  0.571195
e  0.870068  0.935626  0.606911
f  0.037602  0.855193  0.728495
g  0.605366  0.338105  0.696460
h  0.000000  0.090814  0.963927
i  0.688343  0.188468  0.352213
i  0.879000  0.105039  0.900260

In [20]: dfrm['A'].idxmax()
Out[20]: 'i'

In [21]: dfrm.iloc[dfrm['A'].idxmax()]  # .ix instead of .iloc in older versions of pandas
Out[21]: 
          A         B         C
i  0.688343  0.188468  0.352213
i  0.879000  0.105039  0.900260

So here a naive use of idxmax is not sufficient, whereas the old form of argmax would correctly provide the positional location of the max row (in this case, position 9).

This is exactly one of those nasty kinds of bug-prone behaviors in dynamically typed languages that makes this sort of thing so unfortunate, and worth beating a dead horse over. If you are writing systems code and your system suddenly gets used on some data sets that are not cleaned properly before being joined, it's very easy to end up with duplicate row labels, especially string labels like a CUSIP or SEDOL identifier for financial assets. You can't easily use the type system to help you out, and you may not be able to enforce uniqueness on the index without running into unexpectedly missing data.

So you're left with hoping that your unit tests covered everything (they didn't, or more likely no one wrote any tests) -- otherwise (most likely) you're just left waiting to see if you happen to smack into this error at runtime, in which case you probably have to go drop many hours worth of work from the database you were outputting results to, bang your head against the wall in IPython trying to manually reproduce the problem, finally figuring out that it's because idxmax can only report the label of the max row, and then being disappointed that no standard function automatically gets the positions of the max row for you, writing a buggy implementation yourself, editing the code, and praying you don't run into the problem again.

2 of 15
103

You might also try idxmax:

In [5]: df = pandas.DataFrame(np.random.randn(10,3),columns=['A','B','C'])

In [6]: df
Out[6]: 
          A         B         C
0  2.001289  0.482561  1.579985
1 -0.991646 -0.387835  1.320236
2  0.143826 -1.096889  1.486508
3 -0.193056 -0.499020  1.536540
4 -2.083647 -3.074591  0.175772
5 -0.186138 -1.949731  0.287432
6 -0.480790 -1.771560 -0.930234
7  0.227383 -0.278253  2.102004
8 -0.002592  1.434192 -1.624915
9  0.404911 -2.167599 -0.452900

In [7]: df.idxmax()
Out[7]: 
A    0
B    8
C    7

e.g.

In [8]: df.loc[df['A'].idxmax()]
Out[8]: 
A    2.001289
B    0.482561
C    1.579985
Find elsewhere
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-find-a-maximum-value-in-a-pandas-dataframe-column-
How to find a maximum value in a pandas dataframe column ?
It is also possible to find the index corresponding to the max value in the column Age using the pandas function called idxmax · df['Age'].idxmax() returns here · 3 · Then using the index above: df.iloc[3,:] we get · Name Tom Age 43 Name: 3, dtype: object ·
🌐
YouTube
youtube.com › watch
Get Max & Min Value of Column & Index in pandas DataFrame in Python (2 Examples) | idxmax() Function - YouTube
How to find the maximum and minimum value and the corresponding row index position in a pandas DataFrame in the Python programming language. More details: ht...
Published   December 28, 2022
Top answer
1 of 7
26

Use np.argmax

NumPy's argmaxcan be helpful:

>>> df.stack().index[np.argmax(df.values)]
(0, 'A')

In steps

df.values is a two-dimensional NumPy array:

>>> df.values
array([[100,   9,   1,  12,   6],
       [ 80,  10,  67,  15,  91],
       [ 20,  67,   1,  56,  23],
       [ 12,  51,   5,  10,  58],
       [ 73,  28,  72,  25,   1]])

argmax gives you the index for the maximum value for the "flattened" array:

>>> np.argmax(df.values)
0

Now, you can use this index to find the row-column location on the stacked dataframe:

>>> df.stack().index[0]
(0, 'A')

Fast Alternative

If you need it fast, do as few steps as possible. Working only on the NumPy array to find the indices np.argmax seems best:

v = df.values
i, j = [x[0] for x in np.unravel_index([np.argmax(v)], v.shape)]
[df.index[i], df.columns[j]]

Result:

[0, 'A']

Timings

Timing works best for lareg data frames:

df = pd.DataFrame(data=np.arange(int(1e6)).reshape(-1,5), columns=list('ABCDE'))

Sorted slowest to fastest:

Mask:

%timeit df.mask(~(df==df.max().max())).stack().index.tolist()
33.4 ms ± 982 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Stack-idmax

%timeit list(df.stack().idxmax())
17.1 ms ± 139 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Stack-argmax

%timeit df.stack().index[np.argmax(df.values)]
14.8 ms ± 392 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Where

%%timeit
i,j = np.where(df.values == df.values.max())
list((df.index[i].values.tolist()[0],df.columns[j].values.tolist()[0]))

4.45 ms ± 84.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Argmax-unravel_index

%%timeit

v = df.values
i, j = [x[0] for x in np.unravel_index([np.argmax(v)], v.shape)]
[df.index[i], df.columns[j]]

499 µs ± 12 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Compare

d = {'name': ['Mask', 'Stack-idmax', 'Stack-argmax', 'Where', 'Argmax-unravel_index'],
     'time': [33.4, 17.1, 14.8, 4.45, 499],
     'unit': ['ms', 'ms', 'ms', 'ms', 'µs']}


timings = pd.DataFrame(d)
timings['seconds'] = timings.time * timings.unit.map({'ms': 1e-3, 'µs': 1e-6})
timings['factor slower'] = timings.seconds / timings.seconds.min()
timings.sort_values('factor slower')

Output:

                   name    time unit   seconds  factor slower
4  Argmax-unravel_index  499.00   µs  0.000499       1.000000
3                 Where    4.45   ms  0.004450       8.917836
2          Stack-argmax   14.80   ms  0.014800      29.659319
1           Stack-idmax   17.10   ms  0.017100      34.268537
0                  Mask   33.40   ms  0.033400      66.933868

So the "Argmax-unravel_index" version seems to be one to nearly two orders of magnitude faster for large data frames, i.e. where often speeds matters most.

2 of 7
13

Use stack for Series with MultiIndex and idxmax for index of max value:

print (df.stack().idxmax())
(0, 'A')

print (list(df.stack().idxmax()))
[0, 'A']

Detail:

print (df.stack())
0  A    100
   B      9
   C      1
   D     12
   E      6
1  A     80
   B     10
   C     67
   D     15
   E     91
2  A     20
   B     67
   C      1
   D     56
   E     23
3  A     12
   B     51
   C      5
   D     10
   E     58
4  A     73
   B     28
   C     72
   D     25
   E      1
dtype: int64
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-get-the-position-of-max-value-of-a-pandas-series
How to Get the Position of Max Value of a pandas Series?
March 9, 2022 - The argmax() method in the pandas series is used to get the positional index of the maximum value of the series object.
🌐
Codegive
codegive.com › blog › pandas_index_of_max_value.php
Uncover the Max: Master the pandas index of max value (2024) for Faster Data Insights!
A: You can find the pandas index of max value in a specific column by first selecting that column as a Series and then applying idxmax(), like df['ColumnName'].idxmax().
🌐
Iditect
iditect.com › faq › python › find-maximum-value-of-a-column-and-return-the-corresponding-row-values-using-pandas.html
Find maximum value of a column and return the corresponding row values using Pandas
How to find the maximum value of a column in a Pandas DataFrame and return the corresponding row values? Description: Use the idxmax() function to find the index of the maximum value in the column, then use iloc[] to retrieve the corresponding row.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.idxmax.html
pandas.Series.idxmax — pandas 3.0.2 documentation - PyData |
Return indices of the maximum values along the given axis. ... Return index of first occurrence of maximum over requested axis.
🌐
Statology
statology.org › home › how to use idxmax() function in pandas (with examples)
How to Use idxmax() Function in Pandas (With Examples)
June 1, 2021 - You can use the pandas.DataFrame.idxmax() function to return the index of the maximum value across a specified axis in a pandas DataFrame.