0

I am trying compare distances between points (in this case fake people) in longitudes and latitudes.

I can import the data, then convert the lat and long data to radians and get the following output with pandas:

                   lat      long
name                                
Veronica Session  0.200081  0.246723
Lynne Donahoo     0.775020 -1.437292
Debbie Hanley     0.260559 -1.594263
Lisandra Earls    1.203430 -2.425601
Sybil Leef       -0.029293  0.592702

From there i am trying to compare different points and get the distance between them.

I came across a post that seemed to be of use (https://stackoverflow.com/a/40453439/15001056) but I am unable to get this working for my data set.

Any help in calculating the distance between points would be appreciated. Idealy id like to expand and optimise the route once the distance function is working.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
Milo
  • 33
  • 5
  • “Unable to get it working with my dataset” is incredibly vague. Can you describe the problem you’re actually encountering? What’s the error message? Which line of your code is triggering it? What is the code you’re running? What are your expected results based on a few sample calculations you performed by hand? – Paul H Dec 03 '22 at 20:39

1 Answers1

1

I used the function in the answer you linked and it worked fine. Can't confirm that the distance is in the unit you need though.

df['dist'] = \
haversine(df.lat.shift(), df.long.shift(),
             df.loc[1:, 'lat'], df.loc[1:, 'long'], to_radians=False)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Veronica Session    0.200081    0.246723    NaN
Lynne Donahoo       0.775020    -1.437292   9625.250626
Debbie Hanley       0.260559    -1.594263   3385.893020
Lisandra Earls      1.203430    -2.425601   6859.234096
Sybil Leef          -0.029293   0.592702    12515.848878
Yomi
  • 38
  • 1
  • 4
  • Yomi, thanks for the reply. When I use this on my dataframe I recive the following error: "TypeError: cannot do slice indexing on Index with these indexers [1] of type int" I am unsure how to resolve, any help would be apprecisated. For context, my data is in a dataframe prior to executing this line. – Milo Dec 05 '22 at 14:42