1

I want to do this in a proper way:

data = np.array(data)
data =[
[1, 1, 2, 1],
[0, 1, 3, 2],
[0, 2, 3, 2],
[2, 4, 3, 1],
[0, 2, 1, 4],
[3, 1, 4, 1]]

this should become (delete the lines that start with 0):

[1, 1, 2, 1]
[2, 4, 3, 1]
[3, 1, 4, 1]

So far I did it like this:

lines = []
for i in range(0, len(data[0])):
    if data[0,i] != 0:
        lines.append(data[:,i])
lines = np.array(lines)

Then I found this fine method:

mask = 1 <= data[0,:]

and now I want to apply that mask to that array. This Mask reads: [True, False, False, True, False, True]. How do I do that?

1
  • you should specify that you have a numpy array Commented May 24, 2015 at 20:46

3 Answers 3

2

Why not just:

[ar for ar in data if ar[0] != 0]

This assumes that arrays are not empty.

Sign up to request clarification or add additional context in comments.

Comments

1

I presume you have a numpy array based on the data[0,:] and data[0,i] you have in your question and you mean data[:, 0] :

import numpy as np

data = np.array([
    [1, 1, 2, 1],
    [0, 1, 3, 2],
    [0, 2, 3, 2],
    [2, 4, 3, 1],
    [0, 2, 1, 4],
    [3, 1, 4, 1]])

data = data[data[:,0] != 0]
print(data)

Output:

[[1 1 2 1]
 [2 4 3 1]
 [3 1 4 1]]

data[0,:] is the first row [1 1 2 1] not the first column

2 Comments

this is exactly what I was looking for. I was trying something like data[data[:,0].all(1)] but that didn't work :(
@xtlc, you were not too far off ;)
0

Using List comprehension

In [56]: [elem for elem in data if elem[0] !=0]
Out[56]: [[1, 1, 2, 1], [2, 4, 3, 1], [3, 1, 4, 1]]

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.