How do I create a pandas DataFrame with multiple numpy 1d arrays?
I created several np.arrays to do some calculations with them. (All are the same size [100,1]) Now I want to create a pandas Dataframe and each array will be one column of this DF. Array names must be header DataFrame.
In Matlab, I would easily do it like this:
Table = table (array1, array2, array3, ...);
How can I do this in Python?
early!
source to share
Let's say these are your arrays:
arr1, arr2, arr3 = np.zeros((3, 100, 1))
arr1.shape
Out: (100, 1)
You can use hstack to stack them and pass the resulting 2D array to the DataFrame constructor:
df = pd.DataFrame(np.hstack((arr1, arr2, arr3)))
df.head()
Out:
0 1 2
0 0.0 0.0 0.0
1 0.0 0.0 0.0
2 0.0 0.0 0.0
3 0.0 0.0 0.0
4 0.0 0.0 0.0
Or call the columns as arr1
, arr2
...:
df = pd.DataFrame(np.hstack((arr1, arr2, arr3)),
columns=['arr{}'.format(i+1) for i in range(3)])
which gives
df.head()
Out:
arr1 arr2 arr3
0 0.0 0.0 0.0
1 0.0 0.0 0.0
2 0.0 0.0 0.0
3 0.0 0.0 0.0
4 0.0 0.0 0.0
source to share
C solution numpy.concatenate
for 2d array and DataFrame
constructor:
df = pd.DataFrame(np.concatenate([arr1, arr2, arr3], axis=1), columns= ['a','b','c'])
source to share