How to filter data based on index python

Question:

I want to filter the second index of numpy array, but why can’t I save it to filter_arr = []

the code :

import numpy as np

data = [
    [0.0,52.0,33.0,44.0,51.0],
    [0.0,30.0,45.0,12.0,44.0],
    [0.0,67.0,99.0,23.0,78.0]
    ]

arr = np.array(data)

filter_arr = []

for i in range(0,len(arr)):
  if arr[:1,i] > 50:
    filter_arr.append(arr)

filter_arr = np.array(filter_arr)
filter_arr

the filter array should be :

array([[[ 0., 52., 33., 44., 51.],
        [ 0., 67., 99., 23., 78.]],
Asked By: stack offer

||

Answers:

arr[np.max(arr, axis = 1) > 50,:]

or as a function:

def filter_rows(arr, min=50):
    return arr[np.max(arr, axis = 1) > min,:]

based on https://stackoverflow.com/a/70185464/3957794

Answered By: YScharf

You could try below:
This will filter whole row by second index.

import numpy as np

data = [
    [0.0,52.0,33.0,44.0,51.0],
    [0.0,30.0,45.0,12.0,44.0],
    [0.0,67.0,99.0,23.0,78.0]
    ]

arr = np.array(data)

filter_arr = []

for i in range(len(arr)):
    if arr[i,1] > 50:
        filter_arr.append(arr[i])

filter_arr = np.array(filter_arr)
filter_arr
Answered By: Ayla
data = [
    [0.0,52.0,33.0,44.0,51.0],
    [0.0,30.0,45.0,12.0,44.0],
    [0.0,67.0,99.0,23.0,78.0]
    ]

arr = np.array(data)
    
np.array([list(arr[i]) for i in range(len(arr)) if arr[i,1] > 50])

array([[ 0., 52., 33., 44., 51.],
       [ 0., 67., 99., 23., 78.]])
Answered By: Talha Tayyab

Don’t use loops with , the correct approach here is to use numpy slicing/indexing:

filter_arr = arr[arr[:, 1]>50]

Output:

array([[ 0., 52., 33., 44., 51.],
       [ 0., 67., 99., 23., 78.]])
Answered By: mozway
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.