1

As an example, I have a set of records in a 2D numpy.array and I would like to select all records where value in the 3rd column equal 10. Is there a way to do that apart from looping through the array and build a list of the selected records?

Yves Poissant
  • 147
  • 1
  • 10

2 Answers2

0

Here I have created a numpy array.

print(df)

o/p is: array([['A', 'car'],['B', 'bike'],['C', 'car'],['D', 'truck']], dtype=object)

Now to select all rows with car, you can just filter that value from the array

df[df == "car"]

o/p is: array(['car', 'car'], dtype=object)

If you want it to be converted to python list, wrap the statement in list, i.e

list(df[df == "car])

0

Once I knew of the 'filter' concept, I searched some more and found the answer I was looking for in this stackoverflow question.

So in the car example, the filter would be written as df[df[:,1] == 'car']

Yves Poissant
  • 147
  • 1
  • 10