python/pandas/sklearn: getting closest matches from pairwise_distances
<p>I have a dataframe and am trying to get the closest matches using mahalanobis distance across three categories, like:</p>

<pre><code>from io import StringIO
from sklearn import metrics
import pandas as pd

stringdata = StringIO(u"""pid,ratio1,pct1,rsp
    0,2.9,26.7,95.073615
    1,11.6,29.6,96.963660
    2,0.7,37.9,97.750412
    3,2.7,27.9,102.750412
    4,1.2,19.9,93.750412
    5,0.2,22.1,96.750412
    """)

stats = ['ratio1','pct1','rsp']
df = pd.read_csv(stringdata)

d = metrics.pairwise.pairwise_distances(df[stats].as_matrix(),
    metric='mahalanobis')

print(df)
print(d)
</code></pre>

<p>Where that <code>pid</code> column is a unique identifier.</p>

<p>What I need to do is take that <code>ndarray</code> returned by the <code>pairwise_distances</code> call and update the original dataframe so each row has some kind of list of its closest N matches (so <code>pid</code> 0 might have an ordered list by distance of like 2, 1, 5, 3, 4 (or whatever it actually is), but I'm totally stumped how this is done in python.</p>

<pre><code>from io import StringIO
from sklearn import metrics

stringdata = StringIO(u"""pid,ratio1,pct1,rsp
    0,2.9,26.7,95.073615
    1,11.6,29.6,96.963660
    2,0.7,37.9,97.750412
    3,2.7,27.9,102.750412
    4,1.2,19.9,93.750412
    5,0.2,22.1,96.750412
    """)

stats = ['ratio1','pct1','rsp']
df = pd.read_csv(stringdata)

dist = metrics.pairwise.pairwise_distances(df[stats].as_matrix(),
    metric='mahalanobis')
dist = pd.DataFrame(dist)
ranks = np.argsort(dist, axis=1)
df["rankcol"] = ranks.apply(lambda row: ','.join(map(str, row)), axis=1)
df
</code></pre>

python pandas scikit-learn