我有以下列表:
Training_Frame = pca.fit_transform(np_scaled_train)
具有以下形状(2358,4)
我想添加第五列,这个列保存在一个pandas数据框中,这就是我在没有成功的情况下尝试过的:
Training_Frame.append(dataframe_train.iloc[:,-1])
AttributeError: 'numpy.ndarray' object has no attribute 'append'
所以我尝试了以下
saved_frame = np.append(Training_Frame,dataframe_train.iloc[:,-1])
# This works but the result has a weird shape `(11790,)` despite :
np.shape(dataframe_train.iloc[:,-1]) # is (2358,) so I'm expecting or hopping to get a shape like `(2358,5)
`
所以我有点不知道这里有什么问题,任何想法我怎么能这样做?
答案 0 :(得分:2)
如果Training_Frame
和dataframe_train
的长度相同:
Training_Frame = np.column_stack((Training_Frame, dataframe_train.iloc[:,-1].values))
或者你可以从NDArray生成一个DataFrame(如评论中建议的@ayhan):
Training_Frame = pd.DataFrame(Training_Frame).assign(column_name=dataframe_train.iloc[:,-1])