Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparison and indexing series of arrays with length > 1

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 where 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

s[s=='x']

which only works if the elements of s have all exactly one element themselves.

Is there a way to retrieve all elements of s, where s == c, without converting the elements to string?

>Solution :

Use a list comprehension with numpy.array_equal:

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)]

Output:

0    [x]
dtype: object
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading