Python 删除 numpy 中的特定列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16632568/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:13:07  来源:igfitidea点击:
remove a specific column in numpy
提问by user644745
>>> 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]])
I am deleting the 3rd column as
我正在删除第三列
>>> 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.
有没有更好的办法?请认为这是一个新手问题。
采纳答案by Akavall
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]])
回答by jamylak
>>> import numpy as np
>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> np.delete(arr, 2, axis=1)
array([[ 1,  2,  4],
       [ 5,  6,  8],
       [ 9, 10, 12]])
回答by Fredrik Pihl
Something like this:
像这样的东西:
In [7]: x = range(16)
In [8]: x = np.reshape(x, (4, 4))
In [9]: x
Out[9]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
In [10]: np.delete(x, 1, 1)
Out[10]: 
array([[ 0,  2,  3],
       [ 4,  6,  7],
       [ 8, 10, 11],
       [12, 14, 15]])

