How to maintain or recover Dataframe indexing after running Pairwise Distance function?
<p>I'm using sklearn's pairwise distance function, which saved my life when computing a huge matrix, but the problem I'm having is that I lose my indices.</p>

<p>Specifically, I initially have a huge dataframe of 17000 x 300, which I break down into 4 different dataframes based on some class condition.
The 4 separate dataframes keep the original indices, but after I run the pairwise distance function on one of those dataframes, it gives me back a 2d array with correct values but the indices have been reset from 0 up.</p>

<p><strong>How do I keep or recover the original indices</strong>?</p>

<p><code>distance1 = pair.pairwise_distances(df1, metric='euclidean')</code></p>

<p>You can create a DataFrame with matching indices using the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html" rel="nofollow">DataFrame constructor</a> taking the <code>index</code> parameter:</p>

<pre><code>pd.DataFrame(distance1, index=df1.index)
</code></pre>

<p>Furthermore, if you would like to concatenate it horizontally to your existing DataFrame, you can use</p>

<pre><code>pd.concat((df1, pd.DataFrame(distance1, index=df1.index)), axis=1)
</code></pre>

python pandas indexing dataframe distance