获取两个坐标之间的距离时出错

Error while getting the distance between two co-ordinates

我有一个 panda dataframe 具有以下 schema:

customer_id                                     int64
vehicle_type                                   object
pickup_place                                   object
place_category                                 object
how_long_it_took_to_order                      object
pickup_lat                                    float64
pickup_lon                                    float64
dropoff_lat                                   float64
dropoff_lon                                   float64
pickup_coord                                   object
dropoff_coord                                  object
dtype: object

我正在尝试查找上车地点和下车地点之间的距离。所以我最初尝试通过 haversine 公式使用此处 Getting distance between two points based on latitude/longitude 的方法。当我尝试使用

将度数转换为弧度时
df_post['lat1'] = radians(df_post['pickup_lat'])

我收到这个错误:

TypeError: cannot convert the series to <class 'float'>

所以我尝试按照第 3 个回复中的方法,使用 geopy.distance 模块和内置函数,并为此创建了一个经纬度元组。

df_post['pickup_coord']=list(zip(df_post['pickup_lat'],df_post['pickup_lon']))
df_post['dropoff_coord']=list(zip(df_post['dropoff_lat'],df_post['dropoff_lon'])

但是当我尝试内置功能时

df_post['pickup_dropoff_distance']=gd.VincentyDistance(df_post['pickup_coord'],df_post['dropoff_coord']).miles

我收到一个新错误:

ValueError: When creating a Point from sequence, it must not have more than 3 items.

谁能帮我解释为什么会出现这两个错误以及可能的解决方案。

试试这个应该有用

df_post['lat1'] = radians(df_post['pickup_lat'].astype(float))

距离计算器的语法是 geopy.distance.VincentyDistance(coords_1, coords_2).miles,其中 coords_1coords_2 是元组。

要将函数应用于数据框中的每一行,您需要使用 pd.DataFrame.apply:

def distancer(row):
    coords_1 = (row['pickup_lat'], row['pickup_long'])
    coords_2 = (row['dropoff_lat'], row['dropoff_long'])
    return geopy.distance.VincentyDistance(coords_1, coords_2).miles

df_post['pickup_dropoff_distance'] = df_post.apply(distancer, axis=1)