1

I have created numpy zeros array as like below

data = np.zeros(8, dtype={'names':('name', 'token', 'price'),
              'formats':('U10', 'i8', 'f8')})

and assigned value in for loop

for n,a in enumerate(data):
    data[n]['token'] = sList[n].token
    data[n]['name'] = sList[n].name
    data[n]['price'] = sList[n].price

now I want to search data array with the key like where

d = np.where(data[['name']] == 'Ram')
1
  • 5
    You should consider using pandas. Commented Nov 30, 2018 at 8:30

1 Answer 1

1

Setup

data = np.array([('Ram', 0, 0.), ('', 0, 0.), ('', 0, 0.), ('', 0, 0.),
           ('Ram', 0, 0.), ('', 0, 0.), ('', 0, 0.), ('', 0, 0.)],
           dtype=[('name', '<U10'), ('token', '<i8'), ('price', '<f8')])

Look at the difference between when you index using 'name' vs. ['name']:

>>> data['name']
array(['Ram', '', '', '', 'Ram', '', '', ''], dtype='<U10')

>>> data[['name']]
array([('Ram',), ('',), ('',), ('',), ('Ram',), ('',), ('',), ('',)],
      dtype=[('name', '<U10')])

This distinction is clearly defined in the documentation

Accessing Individual Fields

Individual fields of a structured array may be accessed and modified by indexing the array with the field name.

Accessing Multiple Fields

One can index and assign to a structured array with a multi-field index, where the index is a list of field names.

Since you want to compare a string with the values of a single field, you must access this using only the field name:

>>> np.where(data['name'] == 'Ram')
(array([0, 4], dtype=int64),)
Sign up to request clarification or add additional context in comments.

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.