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
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 Comments
np.in1d
.