Home > Software design >  Change first row of numpy array
Change first row of numpy array

Time:01-23

Sorry for the newbie question so i have an array as code below:

import numpy as np

p = np.array([[2,3,0,5],[2,3,4,5],[2,3,4,5],[0,0,0,0]])
p[np.where(p[0]==0)]=100

print(p)

I wanted to change the first rows 0th value to be 100. However the output is:

[[ 2 3 0 5]

[ 2 3 4 5]

[100 100 100 100]

[ 0 0 0 0]]

So it was changing the 3rd row. A bit perplex. Can I use where? What are other suggestions.

Kevin

[[2 3 100 5]

[2 3 4 5]

[2 3 4 5]

[0 0 0 0]]

CodePudding user response:

Directly use indexing:

p[0, p[0]==0] = 100

Updated p:

array([[  2,   3, 100,   5],
       [  2,   3,   4,   5],
       [  2,   3,   4,   5],
       [  0,   0,   0,   0]])
  • Related