Reputation: 1
I need to create an array, correct_data, that is a subset of another one, all_data. This is all_data:
all_data = np.array([[[ 0, 11, 12, 13, 14],
[ 0, 21, 22, 23, 24],
[ 1, 31, 32, 33, 34],
[ 0, 41, 42, 43, 44]]])
The array correct_data will have all the the rows in all_data where the first item is a 1. The way I was doing it is by creating a mask that identifies the rows starting with 1, such as:
correct_mask=all_data[:,0]==1
and then identifies the indexes of such rows (using "where").
Then I create a for loop that takes each row from the index and copy it onto correct_data (using vstack?). Is this a practical way of doing it? If so, I need to create at fist correct_data as an empty array: can this be done?
Alternatively, I have tried to do it in a different way, i.e., delete the rows starting with a 0 from all_data
This is what I was trying:
for row in range(len(all_data)):
this_row=all_data[row]
if (this_row[0])==0:
np.delete(all_data,row,0)
This does delete the rows, but then again how do I store the remaining rows in a new (or the same) array?
Many thanks for any help and clarification (I have a very basic knowledge of Python).
Upvotes: 0
Views: 215
Reputation: 760
correct_data = all_data[all_data[:, :, 0]==1]
The index here: all_data[:, :, 0]==1
returns a boolean array where the 0th element in 3rd dimension is 1. Just use this to index the original array. You will get array([[ 1, 31, 32, 33, 34]])
as the result.
Upvotes: 0
Reputation: 47790
You can just filter it with an index condition:
>>> all_data[all_data[:,:,0]==1]
array([[ 1, 31, 32, 33, 34]])
Upvotes: 1
Reputation: 23
To get all arrays starting with 1 you can use the following:
correct_data = all_data[all_data[:,:, 0] == 1]
It will reassign matching arrays to the desired variable.
Upvotes: 0