Skip to content Skip to sidebar Skip to footer

How To Find The Distance Between 2 Points In 2 Different Dataframes In Pandas?

I've got two dataframes, each with a set of coordinates. Dataframe 1 is a list of biomass sites, with coordinates in columns 'lat' and 'lng'. Dataframe 2 is a list of postcode coor

Solution 1:

To calculate distance between two global coordinates you should use the Haversine Formula, based on this page I have implemented the following method:

import math
def distanceBetweenCm(lat1, lon1, lat2, lon2):
    dLat = math.radians(lat2-lat1)
    dLon = math.radians(lon2-lon1)

    lat1 = math.radians(lat1)
    lat2 = math.radians(lat2)

    a = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLon/2) * math.sin(dLon/2) * math.cos(lat1) * math.cos(lat2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    return c * 6371 * 100000 #multiply by 100k to get distance in cm

You can also modify it to return different units, by multiplying by different powers of 10. In the example a multiplication by 100k results in units in centimeters. Without multiplying the method returns distance in km. From there you could perform more unit conversions if necessary .

Edit: As suggested in the comments, one possible optimization for this would be using power operators instead of regular multiplication, like this:

a = math.sin(dLat/2)**2 + math.sin(dLon/2)**2 * math.cos(lat1) * math.cos(lat2)

Take a look at this question to read more about different speed complexities of calculating powers in python.

Post a Comment for "How To Find The Distance Between 2 Points In 2 Different Dataframes In Pandas?"