python - How to merge 4 columns based on user-defined criterion -
i need merge 4 columns of array single column
array([[0, 0, 0, 1], [1, 0, 0, 0], ..., [0, 1, 0, 0]]) the result should be:
array([3, 0, ..., 1]) in particular, want column indices (starting 0 , ending 3) columns have value 1.
if every row has unique 1 following work:
np.where(a == 1)[1] for example:
>>> = np.array([[0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]]) >>> np.where(a == 1)[1] array([3, 0, 1]) we can see following:
>>> np.where(a == 1) (array([0, 1, 2]), array([3, 0, 1])) array([0, 1, 2]) row indexes having 1 values, , array([3, 0, 1] column indexes. means have 1 values @ coordinates (0, 3), (1, 0), (2, 1). because each row have 1 unique value of one, there 1 column index each row.
Comments
Post a Comment