title: Using Pandas to calculate distance between coordinates from imported csv
body: <p>I am trying to import a .csv that contains two columns of location data (lat/long), compute the distance between points, write the distance to a new column, loop the function to the next set of coordinates, and write the output data frame to a new .csv.  I have the following code written and it </p>
  
  <pre><code>import pandas as pd
  import numpy as np
  pd.read_csv("input.csv")
  
  def dist_from_coordinates(lat1, lon1, lat2, lon2):
  R = 6371  # Earth radius in km
  
  #conversion to radians
  d_lat = np.radians(lat2-lat1)
  d_lon = np.radians(lon2-lon1)
  
  r_lat1 = np.radians(lat1)
  r_lat2 = np.radians(lat2)
  
  #haversine formula
  a = np.sin(d_lat/2.) **2 + np.cos(r_lat1) * np.cos(r_lat2) * np.sin(d_lon/2.)**2
  
  haversine = 2 * R * np.arcsin(np.sqrt(a))
  
  return haversine
  
  lat1 = row['lat1'] #first row of location.lat column here
  lon1 = row['lon1'] #first row of location.long column here
  lat2 = row['lat2'] #second row of location.lat column here
  lon2 = row['lon2'] #second row of location.long column here
  
  print(dist_from_coordinates(lat1, lon1, lat2, lon2), 'km')
  
  df.to_csv('output.csv')
  </code></pre>
  
  <p>I am receiving the following error: Traceback (most recent call last):
    File "Test.py", line 22, in 
      lat1 = row['lat1'] #first row of location.lat column here
  NameError: name 'row' is not defined</p>
  
  <p>Could additional feedback be provided on how to successfully loop this formula through this data?</p>