5

I have a multidimensional numpy array that I want to split based on a particular column.

Ex. [[1,0,2,3],[1,2,3,4],[2,3,4,5]] Say I want to split this array by the 2nd column with the expression x <=2. Then I would get two arrays [[1,0,2,3],[1,2,3,4]] and [[2,3,4,5]].

I am currently using this statement, which I don't think is correct.

splits = np.split(S, np.where(S[:, a] <= t)[0][:1]) #splits S based on t

#a is the column number
5
  • 1
    What's x<=2? Is x your row number? Commented Feb 19, 2016 at 6:27
  • Its just the values in a particular column. Commented Feb 19, 2016 at 6:29
  • 1
    So when you say 2nd column, you mean 1-based. [1, 0, 2, 3], [1, 2, 3, 4] stay together because you compare 0 <= 2 and 2 <= 2. [2, 3, 4, 5] is "split" because you compare 3 <= 2. Correct? Commented Feb 19, 2016 at 6:31
  • @orange Yep exactly that. Commented Feb 19, 2016 at 6:35
  • Please see my answer. Commented Feb 19, 2016 at 6:36

1 Answer 1

6
>>> import numpy as np
>>> a = np.asarray([[1,0,2,3],[1,2,3,4],[2,3,4,5]])
>>> a
array([[1, 0, 2, 3],
       [1, 2, 3, 4],
       [2, 3, 4, 5]])
>>> split1 = a[a[:,1] <= 2, :]
array([[1, 0, 2, 3],
       [1, 2, 3, 4]])
>>> split2 = a[a[:,1] > 2, :]
array([[2, 3, 4, 5]])
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.