CSV文件中有三列存储X、Y和Z的值。这些变量与任何方程式都无关。我想从给定的数据中得到Z值,对应的X和Y值,我从方程中得到。此外,给定的X和Y值来自不同的方程式。因此,它有时与可用数据不匹配。有没有一种方法可以捕捉到数据中最接近的值并获得其Z值?例如,这是存储在CSV文件中的数据。
X | Y | Z |
---|---|---|
-16.332 | 35.523 | 690|
-15.523 | 35.567 | 689
您可以使用最短欧几里得距离:
example.csv
X,Y,Z
-16.332,35.523,690
-15.523,35.567,689
代码:
import csv
with open('example.csv', newline='') as file:
# read all the data converting columns to float
reader = csv.reader(file)
next(reader) # skip header
data = [(float(x),float(y),float(z)) for x,y,z in reader]
def find_nearest(x,y):
def euclidean_distance(row):
return ((x-row[0])**2 + (y-row[1])**2)**.5
data.sort(key=euclidean_distance)
# return Z of first row (shortest distance) after sort
return data[0][2]
print(find_nearest(-16.331,35.522))
输出:
690
也是pandas
解决方案:
import pandas as pd
df = pd.read_csv('example.csv')
def find_nearest(x, y):
# Add/update a column with distance to x,y for each row
df['dist'] = ((x - df['X'])**2 + (y - df['Y'])**2)**.5
# return the Z of smallest distance.
return df.iloc[df.dist.idxmin()].Z
print(find_nearest(-16.331,35.522))