Oscar David
Oscar David

Reputation: 11

Concatenate many 1xn arrays using Python

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?

Upvotes: 1

Views: 566

Answers (3)

titipata
titipata

Reputation: 5389

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

Upvotes: 0

Allen Qin
Allen Qin

Reputation: 19947

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

Upvotes: 0

Good Morning Captain
Good Morning Captain

Reputation: 241

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

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

Upvotes: 2

Related Questions