Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas locate minimum of DataFrame matrix: index, col

Tags:

python

pandas

I'd like to get the index and column name of the minimum value in a pandas DataFrame across all rows and all columns.

I've tried .idxmin but this seems to only work when applied on a column. Ideally the function is a one-liner that doesn't require loops. It seems like a very common problem, but I haven't found a solution yet.

My DataFrame:

      col0, col1, col2
index0 1     2     3 
index1 2     3     4
index2 5     6     7

I'd like to get the index and column for the minimum value across matrix: 1.

so:

some_func(df) = (index0,col0)
like image 996
Willem Avatar asked Dec 18 '22 15:12

Willem


1 Answers

Try this:

df.stack().idxmin()

Out[108]: ('index0', 'col0')
like image 141
Andy L. Avatar answered Dec 28 '22 09:12

Andy L.