Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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
Python 使用sklearn的2个相似网格数据帧中的最近成员_Python_Pandas_Dataframe_Sklearn Pandas - Fatal编程技术网

Python 使用sklearn的2个相似网格数据帧中的最近成员

Python 使用sklearn的2个相似网格数据帧中的最近成员,python,pandas,dataframe,sklearn-pandas,Python,Pandas,Dataframe,Sklearn Pandas,我有两个数据帧: df1: df2: 我想在一定半径内找到最近的df1成员,比如说df2的radius=500。然后我想把这个最接近的c0值放到df2。如果radius=500内没有df1点我想在df2中将c0设置为1.0(x,y)和(xx,yy)分别是df1和df2的平面坐标 所需输出(仅前5行的示例): 我正在考虑将其转换为shapefile,并使用一些空间查询软件。但我相信通过sklearn可以找到有效的解决方案。提前谢谢 如果我正确理解您的要求,您可以使用scipycKDTree。由于C

我有两个数据帧:

df1:

df2:

我想在一定半径内找到最近的df1成员,比如说df2的
radius=500
。然后我想把这个最接近的
c0
值放到df2。如果
radius=500内没有df1点
我想在df2中将
c0
设置为
1.0
(x,y)
(xx,yy)
分别是df1和df2的平面坐标

所需输出(仅前5行的示例):


我正在考虑将其转换为shapefile,并使用一些空间查询软件。但我相信通过
sklearn
可以找到有效的解决方案。提前谢谢

如果我正确理解您的要求,您可以使用scipy
cKDTree
。由于
C/Cython
实现,它的速度非常快。试试看它是否对你有帮助

对于我的
df2
,我只使用
df2
中的前5行。我的
df1
与您的示例
df1
相同。我还假设列
c0
df1
中的最后一列,距离是
Euclidean

from scipy.spatial import cKDTree

df1_cTree = cKDTree(df1[['x','y']])
ix_arr = df1_cTree.query(df2[['xx','yy']], k=1, distance_upper_bound=500)[1]

df2['c0'] = [df1.iloc[x, -1] if x < len(df1) else 1 for x in ix_arr]

Out[438]:
   kat    z0     kr             xx         yy        c0
0  1.0  0.01  0.169  468526.696610  4633654.0  1.253041
1  3.0  0.30  0.214  468757.270633  4633653.0  1.253041
2  1.0  0.01  0.169  468066.930344  4633965.0  1.000000
3  1.0  0.01  0.169  468297.494406  4633964.0  1.000000
4  1.0  0.01  0.169  468528.058460  4633963.0  1.253041
从scipy.spatial导入cKDTree
df1_cTree=cKDTree(df1[['x','y']]
ix_arr=df1_cTree.query(df2[['xx','yy']],k=1,距离上界=500)[1]
df2['c0']=[df1.iloc[x,-1]如果x

注意:df2
的第4行索引从
[468528.0584604633963.0]
df1
[468958.14744344633810]的第0行的距离是
456.4926432
,因此它满足
500
中的条件。因此,它的
c0
不能像您所需的输出那样
1

         kat    z0     kr             xx            yy
0        1.0  0.01  0.169  468526.696610  4.633654e+06
1        3.0  0.30  0.214  468757.270633  4.633653e+06
2        1.0  0.01  0.169  468066.930344  4.633965e+06
3        1.0  0.01  0.169  468297.494406  4.633964e+06
4        1.0  0.01  0.169  468528.058460  4.633963e+06
     ...   ...    ...            ...           ...
1287962  3.0  0.30  0.214  399566.653186  5.115395e+06
1287963  3.0  0.30  0.214  399781.023856  5.115391e+06
1287964  1.0  0.01  0.169  396570.675453  5.115753e+06
1287965  1.0  0.01  0.169  396785.035186  5.115750e+06
1287966  1.0  0.01  0.169  399571.712593  5.115703e+06

[1287967 rows x 5 columns]
         kat    z0     kr             xx            yy  c0
0        1.0  0.01  0.169  468526.696610  4.633654e+06  1.253041
1        3.0  0.30  0.214  468757.270633  4.633653e+06  1.253041
2        1.0  0.01  0.169  468066.930344  4.633965e+06  1.0
3        1.0  0.01  0.169  468297.494406  4.633964e+06  1.0
4        1.0  0.01  0.169  468528.058460  4.633963e+06  1.0
     ...   ...    ...            ...           ...
1287962  3.0  0.30  0.214  399566.653186  5.115395e+06  ...
1287963  3.0  0.30  0.214  399781.023856  5.115391e+06  ...
1287964  1.0  0.01  0.169  396570.675453  5.115753e+06  ...
1287965  1.0  0.01  0.169  396785.035186  5.115750e+06  ...
1287966  1.0  0.01  0.169  399571.712593  5.115703e+06  ...
from scipy.spatial import cKDTree

df1_cTree = cKDTree(df1[['x','y']])
ix_arr = df1_cTree.query(df2[['xx','yy']], k=1, distance_upper_bound=500)[1]

df2['c0'] = [df1.iloc[x, -1] if x < len(df1) else 1 for x in ix_arr]

Out[438]:
   kat    z0     kr             xx         yy        c0
0  1.0  0.01  0.169  468526.696610  4633654.0  1.253041
1  3.0  0.30  0.214  468757.270633  4633653.0  1.253041
2  1.0  0.01  0.169  468066.930344  4633965.0  1.000000
3  1.0  0.01  0.169  468297.494406  4633964.0  1.000000
4  1.0  0.01  0.169  468528.058460  4633963.0  1.253041