0

I have the following code:

# unicorns is a numpy array with several fields
idx = (1, 2, 3, 5, 7)
unicorns=uni[idx]
# now i have only the first, second, third, ... unicorn
print unicorns

However if I want to subselect this unicorn array

unicorns['color'=='white']['Name']

which should give me the names of the unicorns that are white, numpy interprets only the color==white part as False, which goes to 0 and it returns the first entry of my array.

How can I fix this code, so that it does what I want it to, selecting the white unicorns?

I would prefer everything stays as numpy, so I can also select other properties oft the unicorns.

Edit

Here is an example for the arrays:

    unicorns=[(1, black, 0.0, 'Pinky', 1) (2, black, 0.0, 'Winky', 1)
 (3, white, 0.0, 'Lala', 1) (4, white, 0.0, 'Merlin', 1)
 (5, black, 0.0, 'Meriva', 1) (6, white, 0.0, 'Panda', 1)]
    idx = [  0 ,  3  , 6 ] 
1
  • Could you post a minimal version of unicorns that can be used to reproduce the problem? Commented Sep 26, 2012 at 9:20

3 Answers 3

4

I had to modify your array a bit to make it valid Python code. If I converted it properly, then I think what you are looking for is:

unicorns[unicorns['color'] == 'white']['name']

import numpy as np

unicorns=[(1, 'black', 0.0, 'Pinky', 1), (2, 'black', 0.0, 'Winky', 1),
          (3, 'white', 0.0, 'Lala', 1), (4, 'white', 0.0, 'Merlin', 1),
          (5, 'black', 0.0, 'Meriva', 1), (6, 'white', 0.0, 'Panda', 1),
          ]
unicorns = np.array(unicorns,
                    dtype = [('id', '<i4'),
                             ('color', 'S10'),
                             ('val1', '<f4'),
                             ('name', 'S10'),
                             ('val2', '<i4')])

print(unicorns['color'] == 'white')
# [False False  True  True False  True]

print(unicorns[unicorns['color'] == 'white']['name'])
# ['Lala' 'Merlin' 'Panda']
Sign up to request clarification or add additional context in comments.

Comments

1

What you probabply want to use is the numpy.where function. Use it like this:

    >>>unicorns=np.array([[1, "black", 0.0, 'Pinky', 1] ,
                       [2, "black", 0.0, 'Winky', 1],
                       [3, "white", 0.0, 'Lala', 1],
                       [4, "white", 0.0, 'Merlin', 1],
                       [5, "black", 0.0, 'Meriva', 1],
                       [6, "white", 0.0, 'Panda', 1]])
    >>> np.where(unicorns[:,1] == "black")
    (array([0, 1, 4]),)
    >>> unicorns[np.where(unicorns[:,1] == "black")]
    array([['1', 'black', '0.0', 'Pinky', '1'],
    ['2', 'black', '0.0', 'Winky', '1'],
    ['5', 'black', '0.0', 'Meriva', '1']], 
    dtype='|S8')

1 Comment

You're transforming the input as an array of strings... Stick with structured arrays.
1

You can also take a look at pandas which is really awesome for such kinds of slicing and querying operations. Your problem could be solved with it like that:

In [12]: df = pd.DataFrame(unicorns)

In [13]: df.columns = ['id','color','speed','name','tails']

In [14]: df
Out[14]: 
  id  color speed    name tails
0  1  black   0.0   Pinky     1
1  2  black   0.0   Winky     1
2  3  white   0.0    Lala     1
3  4  white   0.0  Merlin     1
4  5  black   0.0  Meriva     1
5  6  white   0.0   Panda     1

In [16]: df[df.color == 'black']
Out[16]: 
  id  color speed    name tails
0  1  black   0.0   Pinky     1
1  2  black   0.0   Winky     1
4  5  black   0.0  Meriva     1

In [17]: df[df.color == 'black'].name
Out[17]: 
0     Pinky
1     Winky
4    Meriva
Name: name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.