Separating list data in data frames to columns and finding Havesine distance

34 Views Asked by At

I have a data frame with column name 0 , 1 , 2, .... each data point contains value of gps coordinated in the format:

            0                          1                         2
  1          [34.033278 73.574869]      [34.033278 73.574869]      ...................
  2          [34.033278 73.574869]      [34.033278 73.574869]      ...................
  3          [34.033278 73.574869]      [34.033278 73.574869]

The rows are clusters and columns are list of coordinates. I want to find the Haversine distance between each datapoint in the cluster.How do I do that? Thank you in advance for your help

I have a function that can calculate haversine distance between coordinates:

def haversine(lon1, lat1, lon2, lat2):
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

    dlon = lon2 - lon1 
    dlat = lat2 - lat1

    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a))

    # Radius of earth in kilometers is 6371
    km = 6371 * c

    return km

But I could not separate the list data in the columns into longitue and latitude with the following code

df[['lat', 'lon']] = df.apply(lambda x: pd.Series(x.iloc[0]), axis=1)
0

There are 0 best solutions below