3

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?

1
  • You can use np.c_ or np.column_stack. For example, np.c_[np.ones((3, 5)), range(3)] Commented Oct 18, 2018 at 0:00

2 Answers 2

5

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 
Sign up to request clarification or add additional context in comments.

Comments

3

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]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.