如何从一个numpy的阵列基于多个条件删除行? [英] How do I remove rows from a numpy array based on multiple conditions?

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

问题描述

我有行4列,成千上万的文件。我想删除的行的第一列中的项目是在一定范围内。例如,
如果我在文件中的数据如下:

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

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 the rows whose first item is between 20 and 25 or between 30 and 35. That means the output I expect is:

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

我怎么能这样做呢?

How could I do this?

推荐答案

如果你想使用 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)
    ))
]

要解释,条件语句如数据[:0] LT; 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的阵列,这些布尔数组。像数据[数据[声明:0]≥ 30] 提取所有的行,其中数据[: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.

最后,我们需要合理的工具来布尔阵列组合元素乘元素。普通的不是语句不工作,因为他们试图布尔数组作为一个整体结合在了一起。幸运的是,numpy的提供了一组这些工具用于在 np.logical_and np.logical_or 的形式使用, np.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天全站免登陆