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)
我想在df1中找到df2的位置,正确答案是[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中的正确位置?
2条答案
按热度按时间7gyucuyw1#
如果你只想匹配非零值,你可以删除它们:
输出量:
如果您想要完全匹配,包括零(每行),您可以使用:用途:
输出:
array([5, 6, 7, 8, 9])
yvt65v4c2#
一种完全匹配的方法: