Home > front end >  Changing Values in Row 0 of Numpy Array
Changing Values in Row 0 of Numpy Array

Time:04-24

I'm having trouble changing the values in a numpy array. I have already set up the array of zeros. Pvfv is present value and future value

pvfv=np.zeros((7,5))
print(pvfv)

Output:

[[ 0.00     0.00     0.00     0.00     0.00]
 [ 0.00     0.00     0.00     0.00     0.00]
 [ 0.00     0.00     0.00     0.00     0.00]
 [ 0.00     0.00     0.00     0.00     0.00]
 [ 0.00     0.00     0.00     0.00     0.00]
 [ 0.00     0.00     0.00     0.00     0.00]
 [ 0.00     0.00     0.00     0.00     0.00]]

Now I want to use np.array change row 0 to look like, keeping the rest of the array constant:

 [[ 0.00     1.00     2.00     3.00     4.00]
  [ 0.00     0.00     0.00     0.00     0.00]
  [ 0.00     .....

CodePudding user response:

Simply use:

import numpy as np

n = 7
p = 5
zeros = np.zeros((n, p))
print("before: \n", zeros, "\n")

row = np.arange(0, p, dtype = float) #no need to copy but just to make things clear.
zeros[0] = row;
print("after: \n", zeros, "\n")

output:

before: 
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]] 

after: 
[[0. 1. 2. 3. 4.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]] 

CodePudding user response:

You could do the top row and then stack it above the rest:

np.vstack((np.arange(5), np.zeros((6, 5))))
  • Related