How to find the distance between two points given longitude and latitude in python-error?

允我心安 提交于 2019-12-13 04:39:11

问题


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().

回答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


来源:https://stackoverflow.com/questions/57004295/how-to-find-the-distance-between-two-points-given-longitude-and-latitude-in-pyth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!