如何根据多个条件从 numpy 数组中删除行? [英] How do I remove rows from a numpy array based on multiple conditions?

查看:50
本文介绍了如何根据多个条件从 numpy 数组中删除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三列和数千行的文件.我想删除那些第一列中的项目在一定范围内的行.例如,如果我的文件中的数据如下:

I have a file with three columns and thousands of rows. I want to remove those rows, whose items in the first column are in a certain range. For example, if the data in my file is as follows:

18  6.215   0.025
19  6.203   0.025
20  6.200   0.025
21  6.205   0.025
22  6.201   0.026
23  6.197   0.026
24  6.188   0.024
25  6.187   0.023
26  6.189   0.021
27  6.188   0.020
28  6.192   0.019
29  6.185   0.020
30  6.189   0.019
31  6.191   0.018
32  6.188   0.019
33  6.187   0.019
34  6.194   0.021
35  6.192   0.024
36  6.193   0.024
37  6.187   0.026
38  6.184   0.026
39  6.183   0.027
40  6.189   0.027

我想删除那些第一项在 20 到 25 或 30 到 35 之间的行.预期的输出是这样的:

I want to remove those rows, whose first item is between 20 and 25 or between 30 and 35. The expected output is thus:

18  6.215   0.025
19  6.203   0.025
26  6.189   0.021
27  6.188   0.020
28  6.192   0.019
29  6.185   0.020
36  6.193   0.024
37  6.187   0.026
38  6.184   0.026
39  6.183   0.027
40  6.189   0.027

我怎么能这样做?

推荐答案

如果你想继续使用 numpy,解决方案并不难.

If you want to keep using numpy, the solution isn't hard.

data = data[np.logical_not(np.logical_and(data[:,0] > 20, data[:,0] < 25))]
data = data[np.logical_not(np.logical_and(data[:,0] > 30, data[:,0] < 35))]

或者,如果您想将所有内容合并为一个语句,

Or if you want to combine it all into one statement,

data = data[
    np.logical_not(np.logical_or(
        np.logical_and(data[:,0] > 20, data[:,0] < 25),
        np.logical_and(data[:,0] > 30, data[:,0] < 35)
    ))
]

解释一下,像 data[:,0] < 这样的条件语句25 创建布尔数组,逐个元素跟踪数组中的条件为真或假.在这种情况下,它会告诉您第一列数据小于 25 的位置.

To explain, conditional statements like data[:,0] < 25 create boolean arrays that track, element-by-element, where the condition in an array is true or false. In this case, it tells you where the first column of data is less than 25.

您还可以使用这些布尔数组索引 numpy 数组.像 data[data[:,0] > 这样的语句30] 提取 data[:,0] > 的所有行30 为真,或第一个元素大于 30 的所有行.这种条件索引是您提取所需行(或列或元素)的方式.

You can also index numpy arrays with these boolean arrays. A statement like data[data[:,0] > 30] extracts all the rows where data[:,0] > 30 is true, or all the rows where the first element is greater than 30. This kind of conditional indexing is how you extract the rows (or columns, or elements) that you want.

最后,我们需要逻辑工具来逐个组合布尔数组.常规的 andornot 语句不起作用,因为它们试图将布尔数组组合为一个整体.幸运的是,numpy 提供了一组这些工具,以 np.logical_andnp.logical_ornp.logical_not 的形式使用.有了这些,我们可以按元素组合我们的布尔数组,以找到满足更复杂条件的行.

Finally, we need logical tools to combine boolean arrays element-by-element. Regular and, or, and not statements don't work because they try to combine the boolean arrays together as a whole. Fortunately, numpy provides a set of these tools for use in the form of np.logical_and, np.logical_or, and np.logical_not. With these, we can combine our boolean arrays element-wise to find rows that satisfy more complicated conditions.

这篇关于如何根据多个条件从 numpy 数组中删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆