1

I'm looping over an array to extract data but it's too slow. Is there a function in numpy that will do this faster? I have to create a new array based on values from an array within an array.

For example, create an array with cars made in USA.

 input: array = [['ford', 'USA'], ['volkswagen', 'Germany'], ['jeep', 'USA']]
 output: new_array = [['ford', 'USA'], ['jeep', 'USA']]
asked Oct 28, 2016 at 13:03

1 Answer 1

1

Assuming an array of string dtype, you can slice out the second column and compare against the string 'USA' to get a boolean array. This boolean array could then be used for indexing into the array using boolean-indexing to select valid rows and give us the desired output.

Thus, the implementation would be simply -

array[array[:,1] == 'USA']
answered Oct 28, 2016 at 13:05
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! @Divakar
How would I write it if I want a result based on two values? For exampel 'jeep' and 'USA'
@elfving Use np.in1d.
@elfving Also have a look at this Q&A to get those indices and then simply index into the list with those indices.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.