Extracting rows based on condition on one column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RockRoll
    New Member
    • Jul 2020
    • 10

    #1

    Extracting rows based on condition on one column

    Hi Everyone,

    Here is an interesting problem I am trying to solve.

    I have a (Nx4) array and want to extract those rows which have their third column's element in certain range. Are there existing capabilities in NumPy? Below is a simple example.

    PS: I know how for loops can be used by comparing each element of col. 3; and saving the rows that meet the condition. But I want to use NumPy here (like slicing etc., that is promisingly fast). In reality, the arrays I use are large and implementing additional loops will sacrifice comp. times.

    For example,:
    Code:
    input = [[1,2,-97,4],
             [5,6,93,8],
             [9,10,-105,12],
             [11,12,105,14]]
     
    output = [[1,2,-97,4], # desired output: rows in which column third's element is greater than -100 and less than 100
              [5,6,93,8]]
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Use a filter

    Code:
    computed = list(filter(lambda x: math.fabs(x[2]) < 100, input))

    Comment

    Working...