1

I have one 1xn array like this:

data = [-2 -1 -3 -5 2 5 8 9 ..... 8]

Now, I want concatenate this with other similar 1xn arrays:

data2 = [0 3 0 0 ..... 5]

final is a big matrix with many rows

[data]
[data2]
...
[data1000]

What is the Python code for this?

5
  • 1
    you have the operative word.... np.concatenate((a, b...)) Commented May 16, 2017 at 3:09
  • data as displayed looks like a (n,) 1d shaped array (no commas, so it's not a list). You want to make a (m,n) array, with mm rows? Commented May 16, 2017 at 3:17
  • exactly finally is a matrix mxn, @NaN np.concatenate() no work, Commented May 16, 2017 at 3:22
  • Try np.array on a list of the 1d arrays: np.array([data, data2, data3, ... ,data1000]) Commented May 16, 2017 at 3:27
  • How is np.concatenate failing? Is it returning an error, or an undesired result? Commented May 16, 2017 at 14:16

3 Answers 3

2
totalData = [data, data2, data3, ... , data1000]

Would be the easiest way to do this if you have no way to iterate over the data.

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

2 Comments

but i need mxn matrix
The list of lists will act like a a matrix when you want to retrieve or change data points. In the example above totalData[2][2]will be equal to the third data entry of data3
0
data = [1,2,3,4]
data2 = [1,2,3]

#put the list of lists to a DataFrame which will make the equal length and fill missing elements with NA. Then use values to get the M*N numpy array.
pd.DataFrame([data,data2]).values
Out[371]: 
array([[  1.,   2.,   3.,   4.],
       [  1.,   2.,   3.,  nan]])

Comments

0

You can also use np.vstack

import numpy as np
data = [-2, -1, -3, -5, 2, 5, 8, 9, 8]
np.vstack([data, data])
# [[-2 -1 -3 -5  2  5  8  9  8]
#  [-2 -1 -3 -5  2  5  8  9  8]]

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.