0

Suppose I have a 2D NumPy array values. I want to add new column to it. New column should be values[:, 19] but lagged by one sample (first element equals to zero). It could be returned as np.append([0], values[0:-2:1, 19]). I tried: Numpy concatenate 2D arrays with 1D array

temp = np.append([0], [values[1:-2:1, 19]])
values = np.append(dataset.values, temp[:, None], axis=1)

but I get:

ValueError: all the input array dimensions except for the concatenation axis 
must match exactly

I tried using c_ too as:

temp = np.append([0], [values[1:-2:1, 19]])
values = np.c_[values, temp]

but effect is the same. How this concatenation could be made. I think problem is in temp orientation - it is treated as a row instead of column, so there is an issue with dimensions. In Octave ' (transpose operator) would do the trick. Maybe there is similiar solution in NumPy?

Anyway, thank you for you time.

Best regards,
Max

1
  • If temp is shape (n,) (1d), you can make a 2d 'column' with temp[:,None] or temp.reshape(n,1). That can then be concatenate with a 2d (n,m) array, on axis=1. The key difference with Octave is that numpy arrays can be true 1d. Commented Aug 19, 2019 at 21:27

2 Answers 2

2
In [76]: values = np.arange(16).reshape(4,4)                                                                 
In [77]: temp = np.concatenate(([0], values[1:,-1]))                                                         
In [78]: values                                                                                              
Out[78]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
In [79]: temp                                                                                                
Out[79]: array([ 0,  7, 11, 15])

This use of concatenate to make temp is similar to your use of append (which actually uses concatenate).

Sounds like you want to join values and temp in this way:

In [80]: np.concatenate((values, temp[:,None]),axis=1)                                                       
Out[80]: 
array([[ 0,  1,  2,  3,  0],
       [ 4,  5,  6,  7,  7],
       [ 8,  9, 10, 11, 11],
       [12, 13, 14, 15, 15]])

Again I prefer using concatenate directly.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to convert the 1D array to 2D as shown. You can then use vstack or hstack with reshaping to get the final array you want as shown:

a = np.array([[1, 2, 3],[4, 5, 6]])
b = np.array([[7, 8, 9]])
c = np.vstack([ele for ele in [a, b]])
print(c)
c = np.hstack([a.reshape(1,-1) for a in [a,b]]).reshape(-1,3)
print(c)

Either way, the output is:

[[1 2 3] [4 5 6] [7 8 9]]

Hope I understood the question correctly

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.