Skip to content Skip to sidebar Skip to footer

Pandas Dataframe: Get Index Of Max Element

I am looking for a way to get both the index and the column of the maximum element in a Pandas DataFrame. Thus far, this is my code: idx = range(0, 50, 5) col = range(0, 50, 5) sco

Solution 1:

You are looking for idxmax :

In [1332]: x
Out[1332]: 
   1  6  11  16  21  26  31  36  41  46
0  0  0   0   0   0   0   0   0   0   0
1  0  0   0   0   0   0   0   0   0   0
2  0  0   5   0   0   0   0   0   0   0
3  0  0   0   0   0   0   0   0   0   0
4  0  0   0   0   0   0   0   0   0   0
5  0  0   0   0   0   0   0   0   0   0
6  0  0   0   0   0   0   0   0   0   0
7  0  0   0   0   0   0   0   0   0   0
8  0  0   0   0   0   0   0   0   0   0
9  0  0   0   0   0   0   0   0   0   0

Row of the max value:

In [1337]: max(x.idxmax())
Out[1337]: 2

Column of the max value (too many maxs):

In [1359]: x.max()[x.max() == x.max(axis=1).max()].index
Out[1359]: Index([u'11'], dtype='object')

Solution 2:

x.max()[x.max() == x.max(axis=1).max()].index 

This works to get the column but max(x.idxmax()) only returns the numerical maximum of the indices themselves, not the index of the maximum value in the table (just got lucky in this example because everything else is 0's). An alternative is:

s = x.max()[x.max() == x.max(index=1).max()].index
s = str(s[0])
max_index = x.idxmax()[s]

Post a Comment for "Pandas Dataframe: Get Index Of Max Element"