Python 如何正确地将一列复制到另一列中(无复制错误)?
我想把一列复制到一个新列中。 我使用以下代码:Python 如何正确地将一列复制到另一列中(无复制错误)?,python,pandas,Python,Pandas,我想把一列复制到一个新列中。 我使用以下代码: df['income10']=df['income'].copy(deep=False) 我得到这个错误: /Users/hairy/ipykernel/ipykernel_launcher.py:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_
df['income10']=df['income'].copy(deep=False)
我得到这个错误:
/Users/hairy/ipykernel/ipykernel_launcher.py:2: SettingWithCopyWarning:
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
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
通过添加浅层副本,我认为我可以防止错误。尝试这样做:
df['income10']=df.loc[:, ['income']]
我重新运行了整个Jupyter笔记本,出于某种原因,它工作了。您的df是另一个数据帧的子集,对吗?使用
df=df.assign(income10=df.income)
。它将创建一个新的对象,而不是一个引用。再说一次,最好在代码中进一步解决这个问题。我仍然可以从中设置CopyWarning。您使用的pandas的版本是什么?pd.\uuuuuuuuu version\uuuuuuuuu:“0.24.0”我的版本是0.22.0,工作正常。。