Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

python - Extract a subset of data from numpy array

I have a 2D numpy array that I need to extract a subset of data from where the value of the 2nd column is higher than a certain value. What's the best way to do this?

E.g. given the array:

array1 = [[1, 5], [2, 6], [3, 7], [4, 8]]

I would want to extract all rows where the 2nd column was higher than 6, so I'd get:

[3, 7], [4, 8]
question from:https://stackoverflow.com/questions/65876437/extract-a-subset-of-data-from-numpy-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Or, even more simply:

a[a[:,1] > 6]

Output:

array([[3, 7], [4, 8]])

Where a is the array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...