jazzytortoise
jazzytortoise

Reputation: 73

How to create a dataframe using arrays generated from a loop as columns in a dataframe

I have a loop printing out arrays of the form [[1,2,3,...100]] [[3,4,5, ...102]]and so on. I need each of the [[---]] to be a separate column in a dataframe. I tried to append them into a list but it is harder to add a 3D array to a dataframe. Could someone please suggest an alternative?

Upvotes: 2

Views: 287

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Try:

a=[[1,2,3],[4,5,6]]
pd.DataFrame(np.array(a).transpose())

Output:

   0  1
0  1  4
1  2  5
2  3  6

Per Comments below:

a = [np.array([[ 1,2,3]]), np.array([[3,4,5]])]

pd.DataFrame(np.vstack(a).T)

Upvotes: 4

Related Questions