title: Python: Computing the distance between two point coordinates using two columns
body: <p>I would like to compute the distance between two coordinates. I know I can compute the haversine distance between two points. However, I was wondering if there is an easier way of doing it instead of creating a loop using the formula iterating over the entire columns (also getting errors in the loop).</p>
  <p>Here's some data for the example</p>
  <pre><code># Random values for the duration from one point to another
  random_values = random.sample(range(2,20), 8)
  random_values
  
  # Creating arrays for the coordinates
  lat_coor = [11.923855, 11.923862, 11.923851, 11.923847, 11.923865, 11.923841, 11.923860, 11.923846]
  lon_coor = [57.723843, 57.723831, 57.723839, 57.723831, 57.723827, 57.723831, 57.723835, 57.723827]
  
  df = pd.DataFrame(
      {'duration': random_values,
       'latitude': lat_coor,
       'longitude': lon_coor
      })
  
  df
  
      duration    latitude    longitude
  0   5           11.923855   57.723843
  1   2           11.923862   57.723831
  2   10          11.923851   57.723839
  3   19         11.923847    57.723831
  4   16         11.923865    57.723827
  5   4          11.923841    57.723831
  6   13         11.923860    57.723835
  7   3          11.923846    57.723827
  </code></pre>
  <p>To compute the distance this is what I've attempted:</p>
  <pre><code># Looping over each row to compute the Haversine distance between two points
  # Earth's radius (in m)
  R = 6373.0 * 1000
  
  lat = df[&quot;latitude&quot;]
  lon = df[&quot;longitude&quot;]
  
  
  for i in lat:
      lat1 = lat[i]
      lat2 = lat[i+1]
      
      for j in lon:
          lon1 = lon[i]
          lon2 = lon[i+1]
          
          dlon = lon2 - lon1
          dlat = lat2 - lat1
          
          # Haversine formula
          a = math.sin(dlat / 2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2)**2
          c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
          distance = R * c
          
          print(distance) # in m
  </code></pre>
  <p>However, this is the error I get:
  <a href="https://i.stack.imgur.com/NLEvt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NLEvt.png" alt="enter image description here" /></a></p>
  <p>The two points to compute the distance should be taken from the same column.</p>
  <p><strong>first distance value:</strong></p>
  <p>11.923855 57.723843 (point1/observation1)</p>
  <p>11.923862 57.723831 (point2/observation2)</p>
  <p><strong>second distance value:</strong></p>
  <p>11.923862 57.723831 (point1/observation2)</p>
  <p>11.923851 57.723839(point2/observation3)</p>
  <p><strong>third distance value:</strong></p>
  <p>11.923851 57.723839(point1/observation3)</p>
  <p>11.923847 57.723831 (point1/observation4)</p>
  <p>... (and so on)</p>