I have an array df
in which each element is a list of 2 numbers. Given an element p = [18, 169]
. I would like to find the indices of such elements p
in df
. Given df
[[[13, 169], [18, 169], [183, 169]],
[[-183, 169], [18, 169], [183, 169]],
[[18, 169], [-18, 169], [183, 169]]]
With (df == p).all(-1)
, I get
array([[False, True, False],
[False, True, False],
[ True, False, False]])
What I want is
[[0, 1],
[1, 1],
[2, 0]]
Could you please elaborate on how to do so?
import numpy as np
df = np.array([[[13, 169], [18, 169], [183, 169]],
[[-183, 169], [18, 169], [183, 169]],
[[18, 169], [-18, 169], [183, 169]]])
p = [18, 169]
ind = (df == p).all(-1)
ind
What you've computed with (df==p).all(-1)
is a mask . They have lots of uses, but you can use that directly to compute the value you want.
# True or false at each coordinate
mask = (df==p).all(-1)
# Extract the coordinates where the mask is True
result = np.argwhere(mask)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.