anon
anon

Reputation: 866

Which is the correct way to efficiently filter a numpy matrix by column values?

I am working with a numpy matrix like this:

[[   0    1    2 ...,   97   98  0]
 [ 100  101  102 ...,  197  198  0]
 [ 200  201  202 ...,  297  298  1]
 ...,
 [9700 9701 9702 ..., 9797 9798 1]
 [9800 9801 9802 ..., 9897 9898 0]
 [9900 9901 9902 ..., 9997 9998 0]]

How can I remove all the rows that have one in the last column of my numpy matrix?:

[[   0    1    2 ...,   97   98  0]
 [ 100  101  102 ...,  197  198  0]
 ...,
 [9800 9801 9802 ..., 9897 9898 0]
 [9900 9901 9902 ..., 9997 9998 0]]

I tried to transform the matrix into a pandas dataframe and filter by the last column:

matrix = pd.DataFrame(data=second_round_mat[1:,1:])
matrix = matrix[matrix['567'] != 1.0]

However, this is not very convinient, and maybe there's a similar way to do that in numpy, thus how can I filter by column value in numpy?

Upvotes: 0

Views: 1348

Answers (1)

w-m
w-m

Reputation: 11232

You can select the rows like this directly in numpy:

matrix = matrix[matrix[:, -1] != 1]

Upvotes: 3

Related Questions