Title sounds more complicated than the facts really are. Given the data
data = [ np.array(['x'], dtype='object'), np.array(['y'], dtype='object'), np.array(['z'], dtype='object'), np.array(['x', 'z', 'y'], dtype='object'), np.array(['y', 'x'], dtype='object'), ] s = pd.Series(data)
I would like to retrieve to elements of
s == np.array(['x']). The obvious way
c = np.array(['x']) s[s==c]
does not work, since there is a ValueError in the comparison, complaining that "’Lengths must match to compare’, (5,), (1,)". I also tried
which only works if the elements of
s have all exactly one element themselves.
Is there a way to retrieve all elements of
s == c, without converting the elements to string?
Use a list comprehension with
c = np.array(['x']) out = s[[np.array_equal(a, c) for a in s]]
Alternative with a
partial function if you need to do this repeatedly (for the shorter syntax):
from functools import partial eq_c = partial(np.array_equal, c) out = s[map(eq_c, s)]
0 [x] dtype: object