0

I have the following two variables:

print('Column vector type %s and shape %s' % (type(target), target[0:X_train.shape[0]].shape))
print('Data frame type %s and shape %s' % (type(X_train), X_train.shape))

and this outputs:

Column vector type <class 'numpy.ndarray'> and shape (87145,)
Data frame type <class 'pandas.core.frame.DataFrame'> and shape (87145, 11)

I'd like to insert the target column vector as first column for this frame / matrix ... how can I do that?

My ultimate goal is to be able to compute the correlation matrix of the response variable appended to the predictor variables or design matrix using the corr function.

SkyWalker
  • 13,729
  • 18
  • 91
  • 187

1 Answers1

1

Use DataFrame.insert:

X_train.insert(0,'target',target) 
print (X_train)

For assign same length array like DataFrame:

X_train.insert(0,'target',target[:X_train.shape[0]]) 
print (X_train)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252