我有两个数据框df1
和df2
.
我想找到df2
与df1
. 一旦从df2
与 中的点对应的点中选择了一个点df1
,将其从df2
数据框中删除(以避免重复中的点df1
)并移动到中的下一个点df1
。
df1
X Y
0 74.7 30.7
1 74.9 30.7
2 75.1 30.7
3 75.3 30.7
4 75.5 30.7
df2
X Y
0 75.80 33.00
1 75.80 33.00
2 76.40 33.00
3 75.80 33.00
4 76.38 33.00
5 76.45 33.00
这个答案非常接近,但为了避免重复值的机会,如何返回最近点的索引以及点本身?
我的代码
from scipy.spatial.distance import cdist
def closest_point(point, points):
""" Find closest point from a list of points. """
return cdist([point], points).argmin(), points[cdist([point], points).argmin()]
dff1=pd.DataFrame()
dff2=pd.DataFrame()
dff1['point'] = [(x, y) for x,y in zip(df1['Y'], df1['X'])]
dff2['point'] = [(x, y) for x,y in zip(df2['Y'], df2['X'])]
P=[]
for x in dff1['point']:
idx,p=closest_point(x, list(dff2['point']))
P.append(p)
dff2.drop(idx,inplace=True)
但不知何故,这是行不通的
如何解决这个问题?请提出一些解决方案。