删除numpy中的特定列 [英] remove a specific column in numpy

查看:98
本文介绍了删除numpy中的特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

我将第3列删除为

>>> np.hstack(((np.delete(arr, np.s_[2:], 1)),(np.delete(arr, np.s_[:3],1))))
array([[ 1,  2,  4],
       [ 5,  6,  8],
       [ 9, 10, 12]])

还有更好的方法吗? 请认为这是一个新手问题.

Are there any better way ? Please consider this to be a novice question.

推荐答案

如果您要删除多个列,只需将要删除的列索引作为列表传递,就像这样:

If you ever want to delete more than one columns, you just pass indices of columns you want deleted as a list, like this:

>>> a = np.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> np.delete(a, [1,3], axis=1)
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])

这篇关于删除numpy中的特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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