根据列表中的条件从 numpy 数组创建一个新数组
假设我有一个如下定义的数组:
data = np.array([('a1v1', 'a2v1', 'a3v1', 'a4v1', 'a5v1'),
('a1v1', 'a2v1', 'a3v1', 'a4v2', 'a5v1'),
('a1v3', 'a2v1', 'a3v1', 'a4v1', 'a5v2'),
('a1v2', 'a2v2', 'a3v1', 'a4v1', 'a5v2'),
('a1v2', 'a2v3', 'a3v2', 'a4v1', 'a5v2'),
('a1v2', 'a2v3', 'a3v2', 'a4v2', 'a5v1'),
('a1v3', 'a2v3', 'a3v2', 'a4v2', 'a5v2'),
('a1v1', 'a2v2', 'a3v1', 'a4v1', 'a5v1'),
('a1v1', 'a2v3', 'a3v2', 'a4v1', 'a5v2'),
('a1v2', 'a2v2', 'a3v2', 'a4v1', 'a5v2'),
('a1v1', 'a2v2', 'a3v2', 'a4v2', 'a5v2'),
('a1v3', 'a2v2', 'a3v1', 'a4v2', 'a5v2'),
('a1v3', 'a2v1', 'a3v2', 'a4v1', 'a5v2'),
('a1v2', 'a2v2', 'a3v1', 'a4v2', 'a5v1')],
dtype=[('a1', '|S4'), ('a2', '|S4'), ('a3', '|S4'),
('a4', '|S4'), ('a5', '|S4')])
如何创建一个函数,按照元组列表 r 中给定的条件按行列出数据元素。
r = [('a1', 'a1v1'), ('a4', 'a4v1')]
我知道可以像这样手动完成:
data[(data['a1']=='a1v1') & data['a4']=='a4v1']
从符合 r 的数据中删除行怎么样?
data[(data['a1']!='a1v1') | data['a4']!='a4v1']
谢谢。
Suppose that I have an array defined by:
data = np.array([('a1v1', 'a2v1', 'a3v1', 'a4v1', 'a5v1'),
('a1v1', 'a2v1', 'a3v1', 'a4v2', 'a5v1'),
('a1v3', 'a2v1', 'a3v1', 'a4v1', 'a5v2'),
('a1v2', 'a2v2', 'a3v1', 'a4v1', 'a5v2'),
('a1v2', 'a2v3', 'a3v2', 'a4v1', 'a5v2'),
('a1v2', 'a2v3', 'a3v2', 'a4v2', 'a5v1'),
('a1v3', 'a2v3', 'a3v2', 'a4v2', 'a5v2'),
('a1v1', 'a2v2', 'a3v1', 'a4v1', 'a5v1'),
('a1v1', 'a2v3', 'a3v2', 'a4v1', 'a5v2'),
('a1v2', 'a2v2', 'a3v2', 'a4v1', 'a5v2'),
('a1v1', 'a2v2', 'a3v2', 'a4v2', 'a5v2'),
('a1v3', 'a2v2', 'a3v1', 'a4v2', 'a5v2'),
('a1v3', 'a2v1', 'a3v2', 'a4v1', 'a5v2'),
('a1v2', 'a2v2', 'a3v1', 'a4v2', 'a5v1')],
dtype=[('a1', '|S4'), ('a2', '|S4'), ('a3', '|S4'),
('a4', '|S4'), ('a5', '|S4')])
How to create a function to list out data elements by row with conditions given in a list of tuples, r.
r = [('a1', 'a1v1'), ('a4', 'a4v1')]
I know that it can be done manually like this:
data[(data['a1']=='a1v1') & data['a4']=='a4v1']
What about removing rows from data that comply with the r.
data[(data['a1']!='a1v1') | data['a4']!='a4v1']
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您想要列出整行,其中给定的列元组等于某个值。在这种情况下,这应该是您想要的,尽管它有点冗长和晦涩:
请注意“嵌套列表”样式索引...这是选择结构化数组的多个列的最简单方法。例如
将产生
然后您可以再次测试您正在检查的值的元组并获得一个一维布尔数组,其中这些列等于这些值。
但是,对于结构化数组,数据类型必须完全匹配。例如
data[['a1', 'a4']] == ('a1v1', 'a4v1')
只是产生False
,所以我们必须创建一个数组我们想要使用与我们正在测试的列相同的数据类型来测试的值。因此,我们必须做一些类似的事情:在我们可以这样做之前:
这会产生我们最初想要的东西:
无论如何,希望这有一定的道理......
If I'm understanding you correctly, you want to list the entire row, where a given tuple of columns is equal to some value. In that case, this should be what you want, though it's a bit verbose and obscure:
Note the "nested list" style indexing... That's the easiest way to select multiple columns of a structured array. E.g.
will yield
You can then test this agains a tuple of the values that you're checking for and get a one-dimensional boolean array where those columns are equal to those values.
However, with structured arrays, the dtype has to be an exact match. E.g.
data[['a1', 'a4']] == ('a1v1', 'a4v1')
just yieldsFalse
, so we have to make an array of the values we want to test using the same dtype as the columns we're testing against. Thus, we have to do something like:before we can do this:
Which yields what we were originally after:
Hope that makes some sense, anyway...