Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在csv文件Python中的位置列表中查找所有位置之间最近的位置_Python_Csv_Dataframe_Geolocation_Location - Fatal编程技术网

在csv文件Python中的位置列表中查找所有位置之间最近的位置

在csv文件Python中的位置列表中查找所有位置之间最近的位置,python,csv,dataframe,geolocation,location,Python,Csv,Dataframe,Geolocation,Location,我在csv文件中有一个位置列表(13000个点),我想找到离每个位置最近的位置(在同一个文件中)。 我从中找到了答案 非常有用,但当我尝试使用数据帧而不是字典时,它会显示错误 我的代码是: 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

我在csv文件中有一个位置列表(13000个点),我想找到离每个位置最近的位置(在同一个文件中)。 我从中找到了答案 非常有用,但当我尝试使用数据帧而不是字典时,它会显示错误

我的代码是:

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))
但它显示了以下错误:


类型错误:字符串索引必须是整数

您的
v['LATITUDE]
等是字符串,在
distance()
函数中,您使用这些参数进行计算。因此,您需要将它们解析为
int()
我将它们更改为float,但仍然存在相同的错误。是否执行了此操作:
返回min(数据,key=lambda x:distance(int(v['LATITUDE'])、int(v['longtude'])、int(x['LATITUDE']、int(x['longtude']))
?@KostasCharitidis返回另一个错误:无法将序列转换为