How do I find the indices of the row and the column for the maximum value in a Pandas dataframe?

Viewed 48

I have a large Pandas dataframe and I want to find out the column and row where the maximum value is (in the entire dataframe). Unfortunately, df.idxmax() only returns the index for the highest value per row/column, not for the entire dataframe. Is there a way to do this?

3 Answers

Using numpy, you can try:

import numpy as np
>>> np.unravel_index(np.argmax(df.values), df.shape)

Assuming all data in your dataframe is numeric you can do it easily by converting the dataframe into numpy array:

import numpy as np
df = np.array(df) # Assuming df is your dataframe name
print(np.unravel_index(df.argmax(), df.shape))

Use:

# calculates the maximum of each row
# returns the index of the max of max 
df.apply(lambda x: max(x.values), axis=1).idxmax()
Related