You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

不同长度DataFrame的经纬度计算异常求助

解决思路与代码实现

你的问题核心是直接对不同长度的DataFrame按索引运算时,Pandas会自动对齐索引,仅索引匹配的行能得到结果,其余返回NaN。要实现每组初始经纬度与每个距离值的全量组合计算,需要先生成两者的笛卡尔积,再逐行计算新坐标。

步骤1:生成经纬度与距离的全量组合

给两个DataFrame添加标识列后,通过交叉合并得到所有可能的组合:

import pandas as pd
import numpy as np

# 给原始经纬度数据添加唯一标识
df['lat_lon_id'] = df.index
# 给距离数据添加唯一标识
new_calc_dist['dist_id'] = new_calc_dist.index

# 交叉合并生成笛卡尔积(所有经纬度+所有距离的组合)
full_combination = pd.merge(df, new_calc_dist, how='cross')

步骤2:计算新经纬度坐标

基于全量组合数据,用你的公式计算新坐标:

r_earth = 6371000
# 计算新纬度(假设距离为正北方向位移)
full_combination['new_lat'] = full_combination['Lat'] + (full_combination['Calculated_Dist_m'] / r_earth) * (180 / np.pi)
# 计算新经度(加入纬度余弦校正)
full_combination['new_lon'] = full_combination['Lon'] + (full_combination['Calculated_Dist_m'] / r_earth) * (180 / np.pi) / np.cos(full_combination['Lat'] * np.pi / 180)

额外说明

  • 合并后的full_combination会包含1000×30000=30,000,000行,完全覆盖你需要的所有组合结果
  • 若不需要中间添加的标识列,可通过full_combination.drop(['lat_lon_id', 'dist_id'], axis=1)删除
  • 当前公式默认距离是正北方向的位移,若实际为其他方向(如正东、任意方位角),需在公式中加入对应方位角参数调整计算逻辑

内容的提问来源于stack exchange,提问作者matrix_season

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:40:26