0

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': ['X'],
                     'Long': [ 28.63615701706],
                     'Lat': [ 41.0693487044612],
                     'Location1': ['Y'],
                     'Long1': [30.7158891385255],
                     'Lat1': [36.963486025471]})  

My solution as suggested Getting distance between two points based on latitude/longitude

 df_TEST['distance']=geopy.distance.geodesic(( df_TEST['Long'], 
 df_TEST['Lat']),(df_TEST['Long1'], df_TEST['Lat1'])).km

My error below please let me know how to fix this. Thank you.

ValueError: The truth value of a Series is ambiguous. Use a.empty, 
a.bool(), a.item(), a.any() or a.all().
melik
  • 1,268
  • 3
  • 21
  • 42

1 Answers1

1

As seen in the Documentation it accepts float tuple and not a pandas series tuple that you are trying to do here, try this instead:

df_TEST['distance']=geopy.distance.geodesic(( float(df_TEST['Long']), 
 float(df_TEST['Lat'])),(float(df_TEST['Long1']), float(df_TEST['Lat1']))).km
Ankit Agrawal
  • 616
  • 9
  • 20
  • 1
    The `()` in `(float)` are unnecessary at best. – mkrieger1 Jul 12 '19 at 09:49
  • And I'm not sure if calling `float()` on a Series object will work. As far as I can tell, it doesn't overload the `__float__` method. – mkrieger1 Jul 12 '19 at 09:53
  • Yes it definitely will work as the series in this case only have a single element for each column, about (float) - I was recently using Java ;) so my bad I have updated the answer now @mkrieger1 – Ankit Agrawal Jul 12 '19 at 10:03
  • 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]}) In this case it is not working – melik Jul 12 '19 at 10:05
  • 1
    Then it should be `float(df_TEST['Long'][0])` etc. – mkrieger1 Jul 12 '19 at 10:06
  • @melik obviously you **cannot typecaste a series to float** as I said above therefore what _mkrieger1_ said worked. – Ankit Agrawal Jul 12 '19 at 10:16
  • geopy.distance.geodesic(float(df_TEST['Long'][0]), float(df_TEST['Lat'][0]),(float(df_TEST['Long1'][0]), float(df_TEST['Lat1'][0]))).km I get the same results what is the problem? – melik Jul 12 '19 at 11:04
  • No problem@melik it's just a clarification for you – Ankit Agrawal Jul 12 '19 at 19:53