Amy Rose - 6 days ago 5
Python Question

# How to call data from a dataframe into Haversine function

I have a dataframe called lat_long which contains the latitude and longitude of some locations. I want to find the difference between each following location. When I use the example haversine function, i get an error. KeyError: ('1', u'occurred at index 0').

``````    1         2
0  -6.081689  145.391881
1  -5.207083  145.788700
2  -5.826789  144.295861
3  -6.569828  146.726242
4  -9.443383  147.220050

def haversine(row):
lon1 = lat_long['1']
lat1 = lat_long['2']
lon2 = row['1']
lat2 = row['2']
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * arcsin(sqrt(a))
km = 6367 * c
return km

lat_long['distance'] = lat_long.apply(lambda row: haversine(row), axis=1)
lat_long
``````

Try this solution:

``````def haversine_np(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)

All args must be of equal length.

"""
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])

dlon = lon2 - lon1
dlat = lat2 - lat1

a = np.sin(dlat/2.0)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2.0)**2

c = 2 * np.arcsin(np.sqrt(a))
km = 6367 * c
return km
``````

Demo:

``````In [17]: df
Out[17]:
lat         lon
0 -6.081689  145.391881
1 -5.207083  145.788700
2 -5.826789  144.295861
3 -6.569828  146.726242
4 -9.443383  147.220050

In [18]: df['dist'] = \
...:     haversine_np(df.lon.shift(), df.lat.shift(), df.ix[1:, 'lon'], df.ix[1:, 'lat'])

In [19]: df
Out[19]:
lat         lon        dist
0 -6.081689  145.391881         NaN
1 -5.207083  145.788700  106.638117
2 -5.826789  144.295861  178.907364
3 -6.569828  146.726242  280.904983
4 -9.443383  147.220050  323.913612
``````