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?
Asked
Active
Viewed 254 times
2 Answers
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])

UnbreakableMystic
- 166
- 3
- 8
-
Using your example, what I'm looking for is a filter that will return [['A', 'car'],['C', 'car']] – Yves Poissant Feb 04 '20 at 18:07
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