1

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.

david
  • 1,255
  • 4
  • 13
  • 26
  • Does this answer your question? [Finding the index of items cotaining 'inf' or 'nan'](https://stackoverflow.com/questions/48592885/finding-the-index-of-items-cotaining-inf-or-nan) – NicoCaldo Jun 24 '22 at 14:13

1 Answers1

0

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.]]
AboAmmar
  • 5,439
  • 2
  • 13
  • 24