user2458922
user2458922

Reputation: 1721

How to form a Pandas DataFrame from multiple multi dimensional numpy array

Given a multi dimensional array x_train_pad.shape = (900, 3) , and x2_train_pad.shape = (900, 7) , and x_train_data.shape = (900, 5). Now the DataFrame to be created should of shape outDF.shape = (900, ( 3+7+5) ) which is 900,15.

Where x_train_data is a DataFrame , I want to retain the column names as is. x_train_pad, and x2_train_pad are Numpy Array, and the resulting DataFrame may have names like x_1,x_2,X_3 ...X2_1,X2_2...X2_7 .

Upvotes: 0

Views: 47

Answers (1)

Dev Khadka
Dev Khadka

Reputation: 5451

import numpy as np
import pandas as pd

arr1 = np.random.rand(10,3)
arr2 = np.random.rand(10,5)
arr3 = np.random.rand(10,7)

c_array = np.c_[arr1,arr2,arr3]
cols = [f"x{j+1}_{i+1}" for j, arr in enumerate([arr1[0],arr2[0],arr3[0]]) 
                    for i,_ in enumerate(arr) ]
df = pd.DataFrame(c_array, columns=cols)
display(df)

Upvotes: 1

Related Questions