title: python: recursively find the distance between points in a group
body: <p>I can apply <code>vincenty</code> in <code>geopy</code> to my <code>dataframe</code> in <code>pandas</code> and determine the distance between the two consecutive machines. However, I want to find the distance between all the machines in the group without repeating.</p>
  
  <p>For example, if I group by company name and there are 3 machines associated with this company, I would want to find the distance between machine 1 and 2, 1 and 3, and (2 and 3) but not calculate the distance between (2 and 1) and (3 and 1) since they are symmetric (identical results).</p>
  
  <pre><code>import pandas as pd
  from geopy.distance import vincenty
  
  df = pd.DataFrame({'ser_no': [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
                  'co_nm': ['aa', 'aa', 'aa', 'bb', 'bb', 'bb', 'bb', 'cc', 'cc', 'cc'],
                  'lat': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                  'lon': [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]})
  
  coord_col = ['lat', 'lon']
  matching_cust = df['co_nm'] == df['co_nm'].shift(1)
  shift_coords = df.shift(1).loc[matching_cust, coord_col]
  # join in shifted coords and compute distance
  df_shift = df.join(shift_coords, how = 'inner', rsuffix = '_2')
  # return distance in miles
  df['dist'] = df_shift.apply(lambda x: vincenty((x[1], x[2]), 
      (x[4], x[5])).mi, axis = 1)
  </code></pre>
  
  <p>This only finds the distance of consecutive machines in the group how can I expand on this to find the distance of all machines in the group?</p>
  
  <p>This code returns:</p>
  
  <pre><code>  co_nm  lat  lon  ser_no      dist
  0    aa    1   21       1       NaN
  1    aa    2   22       2  97.47832
  2    aa    3   23       3  97.44923
  3    bb    4   24       4       NaN
  4    bb    5   25       5  97.34752
  5    bb    6   26       6  97.27497
  6    bb    7   27       7  97.18804
  7    cc    8   28       8       NaN
  8    cc    9   29       9  96.97129
  9    cc   10   30       0  96.84163
  </code></pre>
  
  <hr>
  
  <blockquote>
    <p><strong>Edit:</strong></p>
  </blockquote>
  
  <p>The desired output would find the <em>unique</em> distance combinations for machines related by company; that is, for <code>co_nm aa</code> we would have the distance between ser_no (1,2), (1,3), (2,3), (1,3) and the distance for the machines in <code>co_nm bb</code> and <code>cc</code> as well, but we wouldn't determine the distance of machines in different <code>co_nm</code> groups.</p>
  
  <p>Does this make sense?</p>