How do I remove all zero elements from a NumPy array?

前端 未结 6 455
逝去的感伤
逝去的感伤 2020-12-24 07:14

I have a rank-1 numpy.array of which I want to make a boxplot. However, I want to exclude all values equal to zero in the array. Currently, I solved this by loo

6条回答
  •  梦毁少年i
    2020-12-24 07:28

    A simple line of code can get you an array that excludes all '0' values:

    np.argwhere(*array*)
    

    example:

    import numpy as np
    array = [0, 1, 0, 3, 4, 5, 0]
    array2 = np.argwhere(array)
    print array2
    
    [1, 3, 4, 5]
    

提交回复
热议问题