cyrusgb
cyrusgb

Reputation: 37

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

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?

Upvotes: 2

Views: 62

Answers (3)

nightbot
nightbot

Reputation: 44

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

Upvotes: 1

Muhammad Rasel
Muhammad Rasel

Reputation: 724

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

Upvotes: 1

not_speshal
not_speshal

Reputation: 23146

Using numpy, you can try:

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

Upvotes: 1

Related Questions