Search code examples
pythonarraysnumpyreplacewhere-clause

Conditional Replace within a Column of a Numpy Array


I have a numpy array, for example:

theData= [[0, 1, 1, 1],[0, 1, 3, 1],[3, 4, 1, 3],[0, 1, 2, 0],[2, 1, 0, 0]]

How do I replace all the zeros in the first column with -1?

It's easy to replace all the zeros in the whole array with theData[theData==0] = -1, so I thought something like this would work

theData[theData[:,0] == 0] = -1
theData[:,0 == 0] = -1

but these change all values across the row to -1 for any row in which the first column value is zero. Not my goal, I want to limit the replacement to the first (or whatever) column.

This can obviously be done with a loop. It can also be done by extracting the first column as 1D array, doing the replacement within it, then copying its transpose over the original first column. But I suspect there is a faster and more Pythonic way to do this. Perhaps using np.where, but I can't figure it out.


Solution

  • You can index that column directly as long as you don't build a different object with it. Check the following example:

    theData= np.array([[0, 1, 1, 1],[0, 1, 3, 1],[3, 4, 1, 3],[0, 1, 2, 0],[2, 1, 0, 0]])
    print(theData)
    theData[:,0][theData[:,0] == 0] = -1
    print(theData)
    

    The result is this:

    [[0 1 1 1]
     [0 1 3 1]
     [3 4 1 3]
     [0 1 2 0]
     [2 1 0 0]]
    [[-1  1  1  1]
     [-1  1  3  1]
     [ 3  4  1  3]
     [-1  1  2  0]
     [ 2  1  0  0]]