1

I have a file with various columns. Say

1 2 3 4 5 6

2 4 5 6 7 4

3 4 5 6 7 6

2 0 1 5 6 0

2 4 6 8 9 9

I would like to select and save out rows (in each column) in a new file which have the values in column two in the range [0 - 2].

The answer in the new file should be

1 2 3 4 5 6

2 0 1 5 6 0

Kindly assist me. I prefer doing this with numpy in python.

2
  • can you show us some code that you have so far? ideally so that we can copy the code run it on our computer and make some changes to make it work? Commented Apr 30, 2021 at 12:24
  • s[(s[:,1] <= 2) & (s[:,1] >= 0)] Commented Apr 30, 2021 at 12:30

1 Answer 1

2

For array a, you can use:

a[(a[:,1] <= 2) & (a[:,1] >= 0)]

Here, the condition filters the values in your second column.

For your example:

>>> a
array([[1, 2, 3, 4, 5, 6],
       [2, 4, 5, 6, 7, 4],
       [3, 4, 5, 6, 7, 6],
       [2, 0, 1, 5, 6, 0],
       [2, 4, 6, 8, 9, 9]])
>>> a[(a[:,1] <= 2) & (a[:,1] >= 0)]
array([[1, 2, 3, 4, 5, 6],
       [2, 0, 1, 5, 6, 0]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.