Home > database >  finding the index of removed rows with specific value
finding the index of removed rows with specific value

Time:06-25

I have an array that some rows have inf value in all columns. I need to remved these rows. for this I used the folowing code:

finding_all_infValue=np.asarray(BD[BD.min(axis=1) != np.inf])  

but I also need to know which columns are deleted and I need their index. how can I find the index of reomve columns or save them in a new array? Thanks.

CodePudding user response:

You can track the indices of deleted rows with np.where, and use the same indices to np.delete the corresponding rows.

import numpy as np

BD = np.array([[1.0,1.0,1.0], 
               [np.inf,np.inf,np.inf], 
               [1.0,1.0,1.0], 
               [np.inf,np.inf,np.inf], 
               [1.0,1.0,1.0], 
               [np.inf,np.inf,np.inf]])

indices = np.where((BD==np.inf).all(axis=1))
BD = np.delete(BD, indices, axis=0)

which gives you:

indices
(array([1, 3, 5], dtype=int64),)

BD
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
  • Related