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

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']
4
  • Thank you! @Divakar
    – elfving
    Commented Oct 28, 2016 at 13:09
  • How would I write it if I want a result based on two values? For exampel 'jeep' and 'USA'
    – elfving
    Commented Oct 29, 2016 at 10:22
  • @elfving Use np.in1d.
    – Divakar
    Commented Oct 29, 2016 at 10:27
  • @elfving Also have a look at this Q&A to get those indices and then simply index into the list with those indices.
    – Divakar
    Commented Oct 29, 2016 at 19:41

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.