title: Kmeans Euclidean Distance to Each Centroid Avoid Splitting Features From Rest of DF
body: <p>I have a df:</p>
  
  <pre><code>    id      Type1   Type2   Type3   
  0   10000   0.0     0.00    0.00    
  1   10001   0.0     63.72   0.00    
  2   10002   473.6   174.00  31.60   
  3   10003   0.0     996.00  160.92  
  4   10004   0.0     524.91  0.00
  </code></pre>
  
  <p>I apply k-means to this df and add the resulting cluster to the df:</p>
  
  <pre><code>kmeans = cluster.KMeans(n_clusters=5, random_state=0).fit(df.drop('id', axis=1))
  df['cluster'] = kmeans.labels_
  </code></pre>
  
  <p>Now I'm attempting to add columns to the df for the Euclidean distance between each point (i.e. row in the df) and each centroid:</p>
  
  <pre><code>def distance_to_centroid(row, centroid):
      row = row[['Type1',
                 'Type2',
                 'Type3']]
      return euclidean(row, centroid)
  
  df['distance_to_center_0'] = df.apply(lambda r: distance_to_centroid(r, kmeans.cluster_centers_[0]),1)
  </code></pre>
  
  <p>This results in this error:</p>
  
  <pre><code>---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-34-56fa3ae3df54&gt; in &lt;module&gt;()
  ----&gt; 1 df['distance_to_center_0'] = df.apply(lambda r: distance_to_centroid(r, kmeans.cluster_centers_[0]),1)
  
  ~\_installed\anaconda\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
     6002                          args=args,
     6003                          kwds=kwds)
  -&gt; 6004         return op.get_result()
     6005 
     6006     def applymap(self, func):
  
  ~\_installed\anaconda\lib\site-packages\pandas\core\apply.py in get_result(self)
      140             return self.apply_raw()
      141 
  --&gt; 142         return self.apply_standard()
      143 
      144     def apply_empty_result(self):
  
  ~\_installed\anaconda\lib\site-packages\pandas\core\apply.py in apply_standard(self)
      246 
      247         # compute the result using the series generator
  --&gt; 248         self.apply_series_generator()
      249 
      250         # wrap results
  
  ~\_installed\anaconda\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
      275             try:
      276                 for i, v in enumerate(series_gen):
  --&gt; 277                     results[i] = self.f(v)
      278                     keys.append(v.name)
      279             except Exception as e:
  
  &lt;ipython-input-34-56fa3ae3df54&gt; in &lt;lambda&gt;(r)
  ----&gt; 1 df['distance_to_center_0'] = df.apply(lambda r: distance_to_centroid(r, kmeans.cluster_centers_[0]),1)
  
  &lt;ipython-input-33-7b988ca2ad8c&gt; in distance_to_centroid(row, centroid)
        7                 'atype',
        8                 'anothertype']]
  ----&gt; 9     return euclidean(row, centroid)
  
  ~\_installed\anaconda\lib\site-packages\scipy\spatial\distance.py in euclidean(u, v, w)
      596 
      597     """
  --&gt; 598     return minkowski(u, v, p=2, w=w)
      599 
      600 
  
  ~\_installed\anaconda\lib\site-packages\scipy\spatial\distance.py in minkowski(u, v, p, w)
      488     if p &lt; 1:
      489         raise ValueError("p must be at least 1")
  --&gt; 490     u_v = u - v
      491     if w is not None:
      492         w = _validate_weights(w)
  
  ValueError: ('operands could not be broadcast together with shapes (7,) (8,) ', 'occurred at index 0')
  </code></pre>
  
  <p>This error appears to be happening because <code>id</code> is not included in the <code>row</code> variable in the function <code>distance_to_centroid</code>. To fix this, I could split the df into two parts (<code>id</code> in df1 and the rest of the columns in df2). However, this is very manual, and does not allow for easy changes of columns. Is there a way to get the distance to each centroid into the original df without splitting the original df? In the same vein, is there a better way to find the euclidean distance that wouldn't involve manually entering the columns into the <code>row</code> variable, as well as manually creating however many columns as clusters?</p>
  
  <p><strong>Expected Result:</strong></p>
  
  <pre><code>    id      Type1   Type2   Type3   cluster    distanct_to_cluster_0
  0   10000   0.0     0.00    0.00    1          2.3
  1   10001   0.0     63.72   0.00    2          3.6 
  2   10002   473.6   174.00  31.60   0          0.5 
  3   10003   0.0     996.00  160.92  3          3.7 
  4   10004   0.0     524.91  0.00    4          1.8  
  </code></pre>