【发布时间】:2019-08-19 11:36:13
【问题描述】:
拥有包含 2 个坐标 x_1 和 x_2 且没有值的原始 pandas 数据框:
x_1 x_2
0 0.0 0.0
1 1.0 0.0
2 2.0 0.2
3 2.5 1.5
4 1.5 2.0
5 -2.0 -2.0
以及其他包含坐标点的“校准”数据框:
x_1 x_2 value
0 0.1 0.1 5.0
1 1.0 -2.0 6.0
2 2.0 0.4 3.0
3 2.5 2.5 4.0
4 1.5 1.0 -2.0
5 0.0 0.0 3.0
6 5.6 2.0 5.0
7 7.0 1.0 -3.0
8 8.0 -2.0 -4.0
我想找到原始数据帧的值,基于校准数据帧,使用平面方程,所以我需要找到 3 个最近的点。然后我可以找到原始数据框中每一行的值。如何从其他 pandas 数据框中找到 3 个最近的点?
我的尝试代码如下:
import time
import numpy as np
import scipy
from sklearn.neighbors import NearestNeighbors
# Define input dataframe
df = {'x_1': [0.0,1.0,2.0,2.5,1.5,-2.0],
'x_2': [0.0,0.0,0.2,1.5,2.0,-2.0]}
df = pd.DataFrame(df,columns= ['x_1','x_2'])
print("Dataframe is:\n",df)
# In the below lines define calibration dataframe
print("Defining calibration dataframe...")
calibration = {'x_1': [0.1,1.0,2.0,2.5,1.5,0.0,5.6,7.0,8.0],
'x_2': [0.1,-2.0,0.4,2.5,1.0,0.0,2.0,1.0,-2.0],
'value': [5.0,6.0,3.0,4.0,-2.0,3.0,5.0,-3.0,-4.0]}
calibration = pd.DataFrame(calibration,columns= ['x_1','x_2','value'])
print("Calibration dataframe is:\n",calibration)
# distances = scipy.spatial.distance.cdist(df[['x_1','x_2']], df[['x_1','x_2']], metric='euclidean')
# print(distances)
df['dist'] = np.sqrt( (df.x_1-calibration.x_1)**2 + (df.x_2-calibration.x_2)**2)
df['first_closest_x_1']=0
df['first_closest_x_2']=0
df['value_first_closest']=0
df['second_closest_x_1']=0
df['second_closest_x_2']=0
df['value_second_closest']=0
df['third_closest_x_1']=0
df['third_closest_x_2']=0
df['value_third_closest']=0
# new_df=df.iloc[(df['x_1']-calibration['x_1']).abs().argsort()[:]]
# new_df = pd.DataFrame(mat, index=df['value'], columns=df['value'])
print("New_df:\n",new_df)
print("Values were calculated!")
预期输出如下:
x_1 x_2 first_closest_x_1 first_closest_x_2 value_first_closest second_closest_x_1 second_closest_x_2 value_second_closest third_closest_x_1 third_closest_x_2 value_third_closest
0 0 0 0 0 3 0.1 0.1 5 1.5 1 -2
1 1 0 0.1 0.1 5 0 0 3 2 0.4 3
2 2 0.2 2 0.4 3 1.5 1 -2 0.1 0.1 5
3 2.5 1.5 2.5 2.5 4 1.5 1 -2 2 0.4 3
4 1.5 2 1.5 1 -2 2.5 2.5 4 2 0.4 3
5 0.1 0.1 0 0 3 0.1 0.1 5 1 -2 6
【问题讨论】: