nobru
nobru

Reputation: 43

Numpy add (append) value to each row of 2-d array

I have numpy array of floats with shape (x,14) and I would like to add to the end of each "row" one more value (to each row different value), so that end result has shape (x,15).

We can suppose that I have those values in some list, so that part of the question is also defined.

How to do it with numpy functions?

Upvotes: 3

Views: 8094

Answers (2)

GPrathap
GPrathap

Reputation: 7800

You can use numpy.insert function (https://numpy.org/doc/stable/reference/generated/numpy.insert.html)

a = np.array([[1, 1], [2, 2], [3, 3]])
np.insert(a, 2, 0, axis=1)

Output:

array([[1, 1, 0],
       [2, 2, 0],
       [3, 3, 0]])

Upvotes: 3

hpaulj
hpaulj

Reputation: 231335

Define a 2d array and a list:

In [73]: arr = np.arange(12).reshape(4,3)
In [74]: arr
Out[74]: 
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
In [75]: alist = [10,11,12,13]

Note their shapes:

In [76]: arr.shape
Out[76]: (4, 3)
In [77]: np.array(alist).shape
Out[77]: (4,)

To join alist to arr it needs to have the same number of dimensions, and same number of 'rows'. We can do that by adding a dimension with the None idiom:

In [78]: np.array(alist)[:,None].shape
Out[78]: (4, 1)

Now we can concatenate on the 2nd axis:

In [79]: np.concatenate((arr, np.array(alist)[:,None]),axis=1)
Out[79]: 
array([[ 0,  1,  2, 10],
       [ 3,  4,  5, 11],
       [ 6,  7,  8, 12],
       [ 9, 10, 11, 13]])

column_stack does the same thing, taking care that each input is at least 2d (I'd suggest reading its code.) In the long run you should be familiar enough with dimensions and shapes to do this with plain concatenate.

In [81]: np.column_stack((arr, alist))
Out[81]: 
array([[ 0,  1,  2, 10],
       [ 3,  4,  5, 11],
       [ 6,  7,  8, 12],
       [ 9, 10, 11, 13]])

np.c_ also does this - but note the use of [] instead of (). It's a clever use of indexing notation, convenient, but potentially confusing.

np.c_[arr, alist]
np.r_['-1,2,0', arr, alist]  # for more clever obscurity 

Upvotes: 5

Related Questions