Search code examples
pythonpandasdistancegeopandaseuclidean-distance

Calculate the distance between each GPS points of two lists in python


Let assume that we have a pandas dataframe contain of two columns as ("longitude" and "latitude"), which split by (comma) for example:

longitude latitude
[116.415642, 116.41832, 116.418976, 116.419029, 116.421791, 116.426666, 116.429077, 116.43174, 116.434334, 116.436806] [39.897133, 39.897213, 39.898766, 39.900906, 39.900486, 39.90062, 39.900681, 39.900738, 39.900749, 39.900818]

I am stuck with how can I calculate the distance between each long/lat point to get a new column as distance gap (KM) between each of them as (''gap_dist''). The outputs should be like:

enter image description here

I have found numerous tutorials just to find the distance between a single pair of coordinates (source and destination). So what is the best way to achieve this in python?.

Thanks in advance


Solution

  • if you want the gap between each point and the first point, it goes as this:

    from math import dist
    lats   = [float(lat)  for lat  in df['latitude'][0]]
    longs  = [float(long) for long in df['longtitude'][0]]
    p0 = [lats[0], longs[0]]
    dists = [dist(p0, [x,y]) for x, y in zip(lats, longs)]