Python 如何在数据帧的开头插入列向量?
我有以下两个变量:Python 如何在数据帧的开头插入列向量?,python,pandas,numpy,Python,Pandas,Numpy,我有以下两个变量: 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)) 这就产生了: Column vector type <class 'numpy.ndarray'> and shape (871
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))
这就产生了:
Column vector type <class 'numpy.ndarray'> and shape (87145,)
Data frame type <class 'pandas.core.frame.DataFrame'> and shape (87145, 11)
我想插入目标列向量作为此帧/矩阵的第一列。。。我该怎么做
我的最终目标是能够使用corr函数计算附加到预测变量或设计矩阵的响应变量的相关矩阵 使用:
对于指定与DataFrame相同长度的数组:
X_train.insert(0,'target',target)
print (X_train)
X_train.insert(0,'target',target[:X_train.shape[0]])
print (X_train)