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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.