About Adding Columns in Numpy

Asked 2 years ago, Updated 2 years ago, 69 views

I would like to add "0" to all columns for the array defined below.
What should I do?

 (before adding 0)
arr = np.array ([1, 2, 3], [4, 5, 6], [7, 8, 9])
arr
array([1,2,3],
       [4, 5, 6],
       [7, 8, 9]])
What do you want to do?
array([1,2,3,0],
       [4, 5, 6, 0],
       [7, 8, 9, 0]])
 insert results in a one-dimensional array.
Also, 0 is only the first time.
np.insert(arr, 3,0)
array([1, 2, 3, 0, 4, 5, 6, 7, 8, 9])

python numpy

2022-09-30 21:44

1 Answers

np.insert(arr,3,0,axis=1)

If so,

array([1,2,3,0],
       [4, 5, 6, 0],
       [7, 8, 9, 0]])

is obtained.

of np.insert(arr,3,0,axis=1) 0 is the value to be added
Add axis=1 as column
I think 3 is the third row, but I'm not used to it, so I can't explain it well.


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.