Numpy: argmax over multiple axes without loop

You could do something like this – # Reshape input array to a 2D array with rows being kept as with original array. # Then, get idnices of max values along the columns. max_idx = A.reshape(A.shape[0],-1).argmax(1) # Get unravel indices corresponding to original shape of A maxpos_vect = np.column_stack(np.unravel_index(max_idx, A[0,:,:].shape)) Sample run – In [214]: … Read more

Find row where values for column is maximal in a pandas DataFrame

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 … Read more