根据单个单元格上的条件从numpy数组中删除列 [英] Delete columns from numpy array depending on a condition on a single cell

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

问题描述

最重要的是,抱歉我的英语不好.

Above all, sorry for my bad English.

我有这个数组t:

array([[ 0,  1,  2,  0,  4,  5,  6,  7,  8,  9],
       [ 0, 11,  0, 13,  0, 15,  0, 17, 18,  0]])

我想删除第二行值为空的列.在这里,我想删除第 0、2、4、6 和 9 列,以获得此数组:

I would like to delete the columns where the value of second line is null. Here, I would like to delete the columns 0, 2, 4, 6 and 9, to obtain this array:

array([[  1,   0,   5,   7,  8 ],
       [ 11,  13,  15,  17, 18 ]])

我尝试了 np.sum() 但没有成功.

I tried with np.sum() but didn't succeed.

推荐答案

与 Juh_ 类似,但更具表现力,并避免了一些小的不必要的性能开销.总共 12 个高度 pythonic、明确和明确的字符.这真的是 numpy 101;如果您仍然试图解决这个问题,那么您可以阅读一本 numpy 入门书来帮自己一个忙.

Similar to Juh_, but more expressive, and avoiding some minor unnecessary performance overhead. A grand total of 12 highly pythonic, explicit and unambigious characters. This is really numpy 101; if you are still trying to wrap your head around this, you would do yourself a favor by reading a numpy primer.

import numpy as np
a = np.array([[ 0,  1,  2,  0,  4,  5,  6,  7,  8,  9],
              [ 0, 11,  0, 13,  0, 15,  0, 17, 18,  0]])
print a[:,a[1]!=0]

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

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