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)
.