Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - How could I remove the rows of an array if one of the elements of the row does not satisfy a condition?

I would like to remove the rows of an array when the elements of third column of the array are less than specific amount. For example:

a=np.array([[2331.13,1944.88,23.1379,7,3.18339,0.482105],
[8168.44,1904.70,19.5025,265,4.12642,0.0376510],
[7389.36,1983.97,14.3581,3937,6.04109,0.713416],
[1765.18,1944.29,22.5495,35,2.30717,0.794432],
[2319.33,1946.68,22.4300,25,3.63676,0.0210690],
[785.666,2090.69,14.7940,1095,2.52823,0.999842],
[4071.24,2186.92,22.6616,31,2.79309,0.0312501],
[7082.51,2191.69,23.0122,19,2.53166,0.687001]])

I would like to remove rows which satisfy the following condition:

a[:,2]<15.0

Cheers.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can do:

a[a[:,2]>=15.0, :]

Note the inversion of a[:,2]<15.0 to a[:,2]>=15.0, so that you're describing rows that you want to keep rather than remove.

If inverting your condition isn't as simple as that, you could also use ~:

a[~(a[:,2]<15.0), :]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...