Reputation: 523
As you can see below I have created three arrays that contain different random numbers:
np.random.seed(200)
Array1 = np.random.randn(300)
Array2 = Array1 + np.random.randn(300) * 2
Array3 = Array1 + np.random.randn(300) * 2
data = np.array([Array1, Array2 , Array3])
#data.reshape(data, (Array3, Array1)
mydf = pd.DataFrame(data)
mydf.tail()
My objective is to build a DataFrame with those three arrays. Each array should show its values in a different column. The DataFrame should have three columns and the index. My problem with the above code is that the Dataframe is built in horizontal position instead of vertical position. The DataFrame looks like this:
I have tried to use the reshape function to reshape the numpy array called ”data” but I couldn’t make it work. Any help would be more than welcome. Thanks!
Upvotes: 1
Views: 164
Reputation: 6642
You can use .T
to transpose either the data data = np.array([Array1, Array2 , Array3]).T
or the dataframe mydf = pd.DataFrame(data).T
.
Output:
0 1 2
295 -0.126758 1.697413 0.399351
296 0.548405 1.402154 -4.396156
297 -1.063243 0.279774 -0.636649
298 -0.678952 -2.061554 0.244339
299 -0.527970 -0.290680 -0.930381
Or build a 2D array right away
arr = np.random.randn(300, 3)
arr[:, 1:] *= 2
mydf = pd.DataFrame(arr)
Upvotes: 6