Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 926
Aaron Bramson Avatar asked Apr 10 '18 10:04

Aaron Bramson


2 Answers

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]]
like image 77
armatita Avatar answered Oct 05 '22 23:10

armatita


Try the following:

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

You could also use np.where.

theData[np.where(theData[:,[0]]==0)] = -1
like image 38
miindlek Avatar answered Oct 06 '22 01:10

miindlek