Select certain rows (condition met), but only some columns in Python/Numpy

前端 未结 5 2086
一整个雨季
一整个雨季 2020-12-15 05:29

I have an numpy array with 4 columns and want to select columns 1, 3 and 4, where the value of the second column meets a certain condition (i.e. a fixed value). I tried to f

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 05:56

    If you do not want to use boolean positions but the indexes, you can write it this way:

    A[:, [0, 2, 3]][A[:, 1] == i]
    

    Going back to your example:

    >>> A = np.array([[1,2,3,4],[6,1,3,4],[3,2,5,6]])
    >>> print A
    [[1 2 3 4]
     [6 1 3 4]
     [3 2 5 6]]
    >>> i = 2
    >>> print A[:, [0, 2, 3]][A[:, 1] == i]
    [[1 3 4]
     [3 5 6]]
    

    Seriously,

提交回复
热议问题