使用每个城市的所有可能组合计算5个城市之间的地理距离 [英] Calculate geographical distance between 5 cities with all the possible combinations of each city

查看:350
本文介绍了使用每个城市的所有可能组合计算5个城市之间的地理距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个csv文件,其中包含3列(城市,纬度,经度) 并且我已经使用此代码从该csv文件的python中创建了一个数据框

So I have a csv file which consists of 3 columns (City,Latitude,Longitude) and I have created a data frame in python from this csv file using this code

data = pd.read_csv("lat_long.csv",nrows=10)
Lat = data.lat.tolist()
Lon = data.lon.tolist()
suburb = data.suburb.tolist()
dict={'Latitude':Lat,'Longitude':Lon}
df = pd.DataFrame(dict,index=(suburb))

输出是这个

                                 Latitude   Longitude
AUSTRALIAN NATIONAL UNIVERSITY -35.277272  149.117136
BARTON                         -35.201372  149.095065
DARWIN                         -12.801028  130.955789
DARWIN                         -12.801028  130.955789
PARAP                          -12.432181  130.843310
ALAWA                          -12.378451  130.877014
BRINKIN                        -12.367769  130.869808
CASUARINA                      -12.376597  130.850489
JINGILI                        -12.385761  130.873726
LEE POINT                      -12.360865  130.891349

现在我想要的是从1个城市到其他9个城市的距离的所有可能组合.看起来应该像

Now what I want is all possible combination of distance from 1 city to other 9 cities. It should look like

                                              DISTANCE
AUSTRALIAN NATIONAL UNIVERSITY- BARTON
AUSTRALIAN NATIONAL UNIVERSITY - DARWIN
AUSTRALIAN NATIONAL UNIVERSITY - DARWIN
AUSTRALIAN NATIONAL UNIVERSITY - PARAP

我尝试使用嵌套的for循环来做到这一点,并且可以,但是我想更快一点.

I have tried doing this using nested for loops and it works but I want a bit faster.

推荐答案

我从数据帧开始

    city         Latitude   Longitude
0   AUSTRAL.    -35.277272  149.117136
1   BARTON      -35.201372  149.095065
2   DARWIN      -12.801028  130.955789
3   DARWIN      -12.801028  130.955789
4   PARAP       -12.432181  130.843310
5   ALAWA       -12.378451  130.877014
6   BRINKIN     -12.367769  130.869808
7   CASUARINA   -12.376597  130.850489
8   JINGILI     -12.385761  130.873726
9   LEE_POINT   -12.360865  130.891349

并创建新列,该列仅是创建我们通过将数据框与其自身合并而获得的笛卡尔积的助手.

And create new column which is only a helper to create the cartesian product that we get by merging the dataframe with itself.

df['join'] = 1
df_joined = pd.merge(df, df,on='join')

df_joined['haversine_dist'] = df_joined.apply(lambda x: haversine((x.Latitude_x, x.Longitude_x),(x.Latitude_y,x.Longitude_y)), 1)

结果(仅前5列)

    city_x      Latitude_x  Longitude_x join city_y Latitude_y  Longitude_y haversine_dist
0   AUSTRAL.    -35.277272  149.117136  1   AUSTRAL.    -35.277272  149.117136  0.000000
1   AUSTRAL.    -35.277272  149.117136  1   BARTON  -35.201372  149.095065  8.674473
2   AUSTRAL.    -35.277272  149.117136  1   DARWIN  -12.801028  130.955789  3093.972598
3   AUSTRAL.    -35.277272  149.117136  1   DARWIN  -12.801028  130.955789  3093.972598
4   AUSTRAL.    -35.277272  149.117136  1   PARAP   -12.432181  130.843310  3135.034018
5   AUSTRAL.    -35.277272  149.117136  1   ALAWA   -12.378451  130.877014  3138.077950

这篇关于使用每个城市的所有可能组合计算5个城市之间的地理距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆