我想复制Dataframe的1列,但总是收到警告消息:“试图在DataFrame的切片副本上设置一个值。”我知道这是由于“链式”分配,我只找不到合适的解决方案:
这是第一种无效的方法:
data3['Close2'] = data3['Close']
我尝试了另一种方法,如下所示,但是它们都不起作用:
data3['Close2'] = data3['Close'].copy()
data3['Close2']=data3['Close'].values
data3['Close_input'] = data3['Close'].copy(deep=True)
data3['Close_input'] = data3.loc[:, ('Close')]
data3.Close_input = data3.Close
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
我应该如何修改代码以避免发出警告消息?
谢谢,R
这应该起作用:
data3.loc[:, 'Close2'] = data3.loc[:, 'Close'].copy()