0
import numpy as np

points=[[-1,0],[5,6]]
points = np.array(points)

points_new=np.array([])

for point in points:
    point=np.append(point, 0)
    points_new = np.append(points_new, point)

print(points_new)

I am trying to add 0 to end of elements. Desired output will be

[[-1.  0.  0.]  [5.  6.  0.]]

but I get

[-1.  0.  0.  5.  6.  0.] 

from the code above. Can you tell me where I am doing wrong?

Lyrk
  • 1,936
  • 4
  • 26
  • 48
  • 3
    Does this answer your question? [How to add an extra column to a NumPy array](https://stackoverflow.com/questions/8486294/how-to-add-an-extra-column-to-a-numpy-array) – Kyle Parsons Jul 21 '21 at 14:28
  • 1
    **Read** the docs: If `axis` is not specified, `values` can be any shape and will be flattened before use. – hpaulj Jul 21 '21 at 15:01

5 Answers5

3

I don't know if this is the solution you're looking for, but if it suits your code you can append the list first before making it an array:

import numpy as np

points=[[-1,0],[5,6]]

for point in points:
    point.append(0)

points = np.array(points)
print(points)

This prints:

[[-1  0  0]
 [ 5  6  0]]
Oxin
  • 219
  • 1
  • 11
2

The problem is due to requirement of list in np.append instead of np.array i think

In the backend it will call concatenate so you can try below snippets

We can concatenate compatible numpy arrays, numpy arrays become compatible when their shape size is same.

That is why they single shape points.shape[0] is made into 2D by broadcasting over new dimension using np.newaxis.

Also you dont need new axis if you already define shape appropriately in np.zeroes

>>> import numpy as np
>>> points=[[-1,0],[5,6]]
>>> points = np.array(points)
>>> np.concatenate([points, np.zeros(points.shape[0])[:,np.newaxis]], axis=1)
array([[-1.,  0.,  0.],
       [ 5.,  6.,  0.]])
>>> np.concatenate([points, np.zeros((points.shape[0], 1))], axis=1)
array([[-1.,  0.,  0.],
       [ 5.,  6.,  0.]])
>>> print('trying to generalize')
>>> np.concatenate([points, np.zeros((points.shape[0], *[1] * len(points.shape[1:])))], axis=1)
array([[-1.,  0.,  0.],
       [ 5.,  6.,  0.]])

eroot163pi
  • 1,791
  • 1
  • 11
  • 23
1

It's because of np.append() : here's how it works:

>>>np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
array([1, 2, 3, ..., 7, 8, 9])

Try using usual python lists and do your logic, after that change points_new to np.array

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
oubaydos
  • 194
  • 1
  • 13
1

If you have 2D array and just want new column with zeros on right side you might do

import numpy as np

points=[[-1,0],[5,6]]
points = np.array(points)

points_new=np.hstack([points,np.zeros((points.shape[0],1))])
print(points_new)

output

[[-1.  0.  0.]
 [ 5.  6.  0.]]

Explanation: I create 2D array of appriopiate shape using np.zeros then stacked it horizontally (hstack) with existing array.

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

Concatenate a column of zeros to all the rows as below:

points=[[-1,0],[5,6]]
points = np.array(points)

rows = points.shape[0]
zeros = np.zeros(rows)
points_new = np.c_[points, zeros]

print(points_new)
array([[-1.,  0.,  0.],
   [ 5.,  6.,  0.]])
Madhoolika
  • 396
  • 2
  • 8