Skip to content Skip to sidebar Skip to footer

How Slice Numpy Array By Column Value

I have an array like this numpy array dd =[[0.567 2 0.611] [0.469 1 0.479] [0.220 2 0.269] [0.480 1 0.508] [0.324 1 0.324]] I need 2 seperate array dd[:,1

Solution 1:

You could use numpy fancy indexing:

[~/repo/py]
|32>dd[dd[:,1] == 1]
[32] 
array([[ 0.469,  1.   ,  0.479],
       [ 0.48 ,  1.   ,  0.508],
       [ 0.324,  1.   ,  0.324]])

[~/repo/py]
|33>dd[dd[:,1] == 2]
[33] 
array([[ 0.567,  2.   ,  0.611],
       [ 0.22 ,  2.   ,  0.269]])

Alternatively you could use a list comprehension:

[~/repo/py]
|21>np.array([row for row in dd if row[1] == 1])
[21] 
array([[ 0.469,  1.   ,  0.479],
       [ 0.48 ,  1.   ,  0.508],
       [ 0.324,  1.   ,  0.324]])

[~/repo/py]
|22>np.array([row for row in dd if row[1] == 2])
[22] 
array([[ 0.567,  2.   ,  0.611],
       [ 0.22 ,  2.   ,  0.269]])

edit:

how to time these things in ipython:

[~/repo/py]
|36>timeit dd[dd[:,1] == 1]
100000 loops, best of 3: 6 us per loop

[~/repo/py]
|37>timeit np.array([row for row inddif row[1] == 1])
100000 loops, best of 3: 11.5 us per loop

Post a Comment for "How Slice Numpy Array By Column Value"