remove a specific column in numpy

后端 未结 3 803
忘掉有多难
忘掉有多难 2020-12-15 18:16
>>> 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条回答
  • 2020-12-15 18:48

    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]])
    
    0 讨论(0)
  • 2020-12-15 18:49
    >>> 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]])
    
    0 讨论(0)
  • 2020-12-15 18:58

    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]])
    
    0 讨论(0)
提交回复
热议问题