Home > Software engineering >  How how to calculate haversine cross-distance between to pandas dataframe
How how to calculate haversine cross-distance between to pandas dataframe

Time:03-30

Here's my dataset B

   index       lon        lat
0   0   107.071969  -6.347778
1   1   110.431361  -7.773489
2   2   111.978469  -8.065442

and dataset C

    index      lon        lat
5   5   112.340919  -7.520442
6   6   107.179119  -6.291131
7   7   106.807442  -6.437383

What I need the output is (calculation is completely ramdom)

           0       1        2 
  5      4.5     7.7      7.8 
  6      5.9     7.9      2.8
  7      6.7     7.7     10.2

What I try

from haversine import haversine
haversine(B,C)

And the outpout

ValueError                                Traceback (most recent call last)
<ipython-input-55-3078154d1efc> in <module>
      1 from haversine import haversine
----> 2 haversine(B,C)

~/.local/lib/python3.6/site-packages/haversine/haversine.py in haversine(point1, point2, unit)
     86 
     87     # unpack latitude/longitude
---> 88     lat1, lng1 = point1
     89     lat2, lng2 = point2
     90 

ValueError: too many values to unpack (expected 2)

CodePudding user response:

Check your output distances, what units are they? I converted mine to kilometers. You can check using an online distance calculator if you wanted. Let me know

import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import haversine_distances
pd.DataFrame(haversine_distances(np.radians(df1[['lat','lon']]),np.radians(df2[['lat','lon']]))* 6371,index=df1.index, columns=df2.index)



    5           6           7
0  596.019968   13.413123   30.882602
1  212.317223  394.942014  426.564799
2   72.573637  565.020998  598.409848
  • Related