I have a list of locations in a csv file(which is 13000 points) and I want to find nearest location(in the same file) to each of them. I found the answer in Find the closest latitude and longitude very helpful but when I try to use a data frame instead of dictionary it shows error.
my code is :
from math import cos, asin, sqrt
import os
import pandas as pd
os.chdir('E:\My_Desktop\smart_capex')
sites=pd.read_csv('EXISTING.csv')
def distance(lat1, lon1, lat2, lon2):
p = 0.017453292519943295
a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p)*cos(lat2*p) * (1-cos((lon2-lon1)*p)) / 2
return 12742 * asin(sqrt(a))
def closest(data, v):
return min(data, key=lambda x:
distance(v['LATITUDE'],v['LONGTITUDE'],x['LATITUDE'],x['LONGTITUDE']))
tempDataList = sites
v = sites
print(closest(tempDataList, v))
but it shows below error:
TypeError: string indices must be integers