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']]
elfving
  • 21
  • 1
  • 3

1 Answers1

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']
Divakar
  • 218,885
  • 19
  • 262
  • 358