输出是在NAN错误的距离不计算

时间:2018-03-14 09:16:05

标签: python pandas geodesic-sphere

Data input:
cell_id         Lat_Long    Lat         Long        
15327    28.46852_76.99512  28.46852  76.99512
52695   28.46852_76.99512   28.46852    76.99512
52692   28.46852_76.99512   28.46852    76.99512
29907   28.46852_76.99512   28.46852    76.99512
29905   28.46852_76.99512   28.46852    76.99512

应用Geodesic并找出距离b / w cell_id但它会创建     距离列,但所有值都是NAN。

 Code:    
 Geo = Geodesic.WGS84
 n=len(df3)-1
 for i in range(0, n):
    #df3=df3['Lat'].astype(float)
     Lat1=float(df3['Lat'].iloc[i])
     Long1=float(df3['Long'].iloc[i])
     Lat2=float(df3['Lat'].iloc[i+1])
     Long2=float(df3['Long'].iloc[i+1])
     df3['dis']=pd.Series(Geo.Inverse( Lat1, Long1, Lat2, Long2))
     if(i==n):
         df3['dis']=pd.Series()
     print df3

输出:

            cellid            Lat_Long         Lat        Long      dis

            15327         28.46852_76.99512    28.46852    76.99512  NaN
            52695         28.46852_76.99512    28.46852    76.99512  NaN
            52692         28.46852_76.99512    28.46852    76.99512  NaN
            29907         28.46852_76.99512    28.46852    76.99512  NaN
            29905         28.46852_76.99512    28.46852    76.99512  NaN
            39502           28.4572_77.0008     28.4572     77.0008  NaN

      what is the problem in this code.

1 个答案:

答案 0 :(得分:0)

Geo.Inverse返回字典而不是单个值。查看documentation

使用键s12 – the distance from the first point to the second in meters

返回距离
n = len(df) - 1
for i in range(0, n):
    Lat1 = float(df['Lat'].iloc[i])
    Long1 = float(df['Long'].iloc[i])
    Lat2 = float(df['Lat'].iloc[i + 1])
    Long2 = float(df['Long'].iloc[i + 1])
    df['dis'] = Geo.Inverse(Lat1, Long1, Lat2, Long2)["s12"]
    if (i == n):
        df['dis'] = None

这将导致:

    cell_id       Lat_Long       Lat          Long      dis
0   15327   28.46852_76.99512   28.46852    76.99512    0.0
1   52695   28.46852_76.99512   28.46852    76.99512    0.0
2   52692   28.46852_76.99512   28.46852    76.99512    0.0
3   29907   28.46852_76.99512   28.46852    76.99512    0.0
4   29905   28.46852_76.99512   28.46852    76.99512    0.0

顺便问一下你必须使用geodesc吗?你可以用一个接受numy.ndarray的矢量化替换距离函数,你只需要传递Lat和Long列,然后移动它们的移位版本。这将大大提高性能。

检查this PyCon技术谈论矢量化函数,幸运的是你;它是关于计算两点之间的距离!