Python 使用Pandas和Geopy计算两组lat/long坐标之间的距离
我尝试使用两个不同的数据帧,每个数据帧具有不同的一组横向/纵向坐标,以使用Geopy计算它们之间的距离Python 使用Pandas和Geopy计算两组lat/long坐标之间的距离,python,pandas,geopy,Python,Pandas,Geopy,我尝试使用两个不同的数据帧,每个数据帧具有不同的一组横向/纵向坐标,以使用Geopy计算它们之间的距离 from geopy import distance def dist_calc (row): start = (row['Lat_1' ], row['Long_1']) stop = (row['Lat_2'], row['Long_2']) return distance.great_circle(start, stop).km df['distance']
from geopy import distance
def dist_calc (row):
start = (row['Lat_1' ], row['Long_1'])
stop = (row['Lat_2'], row['Long_2'])
return distance.great_circle(start, stop).km
df['distance'] = df.apply (lambda row: dist_calc (row), axis=1)
我一直得到下面的错误。
我也尝试过忽略_index=True
KeyError: ('Lat_2', 'occurred at index 0')
我是否需要合并或连接数据帧来完成此操作?或者如何使此代码正常工作?先运行dfs,然后运行函数:
new_df = pd.concat([df1, df2], axis=1)
def dist_calc (row):
start = (row['Lat_1' ], row['Long_1'])
stop = (row['Lat_2'], row['Long_2'])
return distance.great_circle(start, stop).km
new_df['distance'] = new_df.apply (lambda row: dist_calc (row), axis=1)
你从错误信息中了解到了什么?哦,我知道代码不起作用,因为它只调用1号数据帧。对不起?此外,您能否共享所有相关代码和数据?请参阅:。