如何找到一个二维数组到另一个二维数组中相同行的位置?

问题描述 投票:0回答:1
import numpy as np

# Create two sample dataframes
df1 = np.array([[0.000000,0.000000,0.000000],
[0.090000,0.000000,0.000000],
[0.190000,0.000000,0.000000],
[0.280000,0.000000,0.000000],
[0.380000,0.000000,0.000000],
[0.470000,0.000000,0.000000],
[0.570000,0.000000,0.000000],
[0.660000,0.000000,0.000000],
[0.760000,0.000000,0.000000],
[0.850000,0.000000,0.000000]])

df2 = np.array([[0.470000,0.000000,0.000000],
[0.570000,0.000000,0.000000],
[0.660000,0.000000,0.000000],
[0.760000,0.000000,0.000000],
[0.850000,0.000000,0.000000]
])

df3 = np.where(np.isclose(df1[:, np.newaxis], df2))[0]

print(df3)

我想找到df2在df1中的位置,正确答案是

[5, 6, 7, 8, 9]
但Python的输出是
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9]
这是没有意义的。如何更改代码以正确找到 df2 在 df1 中的位置?

python python-3.x numpy numpy-ndarray
1个回答
0
投票

你必须去掉零,它们也是匹配的:

np.where(np.isclose(np.where(df1!=0, df1, np.nan)[:, None], df2))[0]

输出:

array([5, 6, 7, 8, 9])
© www.soinside.com 2019 - 2024. All rights reserved.