Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove numpy rows which don't contain whole numbers

Tags:

python

numpy

I have an array

a = np.array([[1, 2.0, 3],
              [1.23,2.5,3],
              [1,4.6,4.9]])

I want to check each value in the 2nd column for a whole number and keep that row if it is a whole number. I have tried this.

for i in range(0,len(a)):
    try:
        if a[i,1].is_integer()==False:
            a=np.delete(a,(i),axis=0)
    except IndexError:
        continue

Output:

array([[1. , 2. , 3. ],
       [1. , 4.6, 4.9]])

Desired output: array([[1. , 2. , 3. ]])

Reason for failure: When a row is deleted, i the row below moves up. Getting the desired output without using delete is also okay.

like image 731
Bibek Mishra Avatar asked Jan 28 '23 02:01

Bibek Mishra


1 Answers

output = a[np.mod(a[:, 1], 1) == 0]
like image 125
Mehdi Avatar answered Feb 12 '23 06:02

Mehdi