我得到了一个数据框,里面有一些地方的经纬度。想象一下城市。在
df = pd.DataFrame([{'city':"Berlin", 'lat':52.5243700, 'lng':13.4105300},
{'city':"Potsdam", 'lat':52.3988600, 'lng':13.0656600},
{'city':"Hamburg", 'lat':53.5753200, 'lng':10.0153400}]);
现在我试着让所有的城市都围绕着另一个。假设所有城市距离柏林500公里,距离汉堡500公里,等等。我可以复制原始数据帧并用距离函数将两者连接起来。在
中间结果如下:
^{pr2}$分组(减少)后的最终结果应该是这样的。备注:如果值列表包含城市的所有列,则会很酷。在
Berlin --> [Potsdam, Hamburg]
Potsdam --> [Berlin, Hamburg]
Hamburg --> [Berlin, Potsdam]
或者仅仅是一个城市周围500公里的城市数量。在
Berlin --> 2
Potsdam --> 2
Hamburg --> 2
由于我对Python比较陌生,所以我希望有任何起点。我对哈弗辛距离很熟悉。但不确定在Scipy或Pandas中是否有有用的距离/空间方法。在
很高兴你能给我一个起点。到目前为止,我试着遵循this post。在
更新:这个问题背后的最初想法来自Two Sigma Connect Rental Listing Kaggle Competition。这样做的目的是让这些上市公司在另一家上市公司周围获得1亿美元的收益。其中a)表示一个密度,因此是一个流行区域;b)如果比较地址,您可以发现是否存在交叉,因此是一个噪声区域。因此,您不需要完整的项对项关系,因为您不仅需要比较距离,还需要比较地址和其他元数据。附言:我不会上传解决方案到Kaggle。我只想学习。在
您可以使用:
首先需要与^{} 交叉联接,通过^{} 删除}中具有相同值的行:
^{pr2}$city_x
和{然后应用haversine函数:
过滤距离:
最后创建}:
list
或使用groupby
获得{也可以使用^{} :
更新:我建议首先建立一个距离数据帧:
有效产生成对距离测向(无重复):
^{pr2}$旧答案:
下面是一个位优化的版本,它使用scipy.spatial.distance.pdist方法:
这给了我们:
让我们统计一下距离大于30的城市数量:
相关问题 更多 >
编程相关推荐