Reputation: 11982
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)
scores = pd.DataFrame(np.zeros((len(idx), len(col))), index=idx, columns=col, dtype=float)
scores.loc[11, 16] = 5 #Assign a random element
This gives me the following DataFrame:
| 1 6 11 16 21 26 31 36 41 46
------------------------------------------
1 | 0 0 0 0 0 0 0 0 0 0
6 | 0 0 0 0 0 0 0 0 0 0
11| 0 0 0 5 0 0 0 0 0 0
16| 0 0 0 0 0 0 0 0 0 0
21| 0 0 0 0 0 0 0 0 0 0
26| 0 0 0 0 0 0 0 0 0 0
31| 0 0 0 0 0 0 0 0 0 0
36| 0 0 0 0 0 0 0 0 0 0
41| 0 0 0 0 0 0 0 0 0 0
46| 0 0 0 0 0 0 0 0 0 0
After that, I use the unstack
method:
unstacked = scores.unstack().copy()
unstacked.sort(ascending=False)
This gives me:
16 11 5
46 46 0
16 31 0
11 31 0
36 0
...
How can I get the index and column of the maximum value? I would like to get something along the lines of an array or tuple containing (16, 11)
.
Upvotes: 5
Views: 19242
Reputation: 110
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]
Upvotes: 2
Reputation: 16134
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 max
s):
In [1359]: x.max()[x.max() == x.max(axis=1).max()].index
Out[1359]: Index([u'11'], dtype='object')
Upvotes: 9