Question: I've been browsing for an answer to my issue but I can't seem to find a suitable solution. I have a dataframe with distances (NxN cells) and I find the minimum distance of the whole dataframe with:
 min\_distance = distances.values.min()
Now I need to find the location (which row and which column of the dataframe) of the min\_distance. Any ideas?

EDIT

*Minimal code*

```
import numpy as np
import pandas as pd

distances=[]
for i in range(5):
    distances.append([])
    for j in range(5):
        distances[i].append(np.random.randint(10))
distances=pd.DataFrame(distances)
min_distance = distances.values.min()
print "Minimum=", min_distance
print "Location of minimum value="

```

Answer: 
I depends on what form you want your result in. But a very straight forward approach would be to use `stack` and `idxmin`.

Like so:

### Setup

```
import pandas as pd

df = pd.DataFrame([[2, 2, 2], [2, 1, 2], [2, 2, 2]],
                  columns=list('ABC'), index=list('abc'))

print df

   A  B  C
a  2  2  2
b  2  1  2
c  2  2  2

```

We should expect the min to be `1` and the location to be row `b` columns `B`

Solution
========

```
df.stack().idxmin()

('b', 'B')

```

Now you could manipulate this to deliver this any other way. This just happens to deliver a tuple.
Generate example:

```
N = 4
df = pd.DataFrame(np.random.rand(N,N))

```

Find minimal index of flattened dataframe: 

```
idx_min = df.values.flatten().argmin()

```

Simple arithmetic to get the row and column numbers back:

```
row = ((idx_min + 1) // N) - 1
column = idx_min - (row * N)

```