Reputation: 1332
I have two locations with longitude and latitude given and I would like to get the distance between these points in python. My data set looks like below:
df_TEST = pd.DataFrame({'Location': ['X1','X2'],
'Long': [ 28.63615701706,76],
'Lat': [ 41.0693487044612,54],
'Location1': ['Y1','Y2'],
'Long1': [30.7158891385255,65],
'Lat1': [36.963486025471,45]})
I would like to add a new column to this dataframe with below code but it works only for one row. I have a huge data set and I would like to add this column without do loop. Solution suggested from How to find the distance between two points given longitude and latitude in python-error? How can I do this?
df_TEST['distance']=geopy.distance.geodesic(float(df_TEST['Long'] .
[0]), float(df_TEST['Lat'][0]),(float(df_TEST['Long1'] .
[0]),float(df_TEST['Lat1'][0]))).km
Upvotes: 0
Views: 301
Reputation: 6665
I may have missed something but what about
>>> df_TEST['distance_km'] = df_TEST.apply(
lambda r: geopy.distance.geodesic(
r.Long, r.Lat, r.Long1, r.Lat1
).km,
axis=1
)
>>> df_TEST
Location Long Lat Location1 Long1 Lat1 distance_km
0 X1 28.636157 41.069349 Y1 30.715889 36.963486 3221.113126
1 X2 76.000000 54.000000 Y2 65.000000 45.000000 5904.462593
Upvotes: 1
Reputation: 394
You can use the apply()
function on your DataFrame, to apply a function to each row of the DataFrame.
Here is the code:
def distance(row):
return (geopy.distance.geodesic((row['Long'], row['Lat']),
row['Long1'], row['Lat1']).km)
df_TEST['distance']=df_TEST.apply(distance, axis=1)
Upvotes: 1