如何正确地将一列复制到另一列(没有复制错误)?

时间:2019-01-31 19:13:30

标签: 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_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

通过添加浅表副本,我认为我可以避免该错误。

2 个答案:

答案 0 :(得分:1)

尝试这样做:

df['income10']=df.loc[:, ['income']]

答案 1 :(得分:0)

我重新运行了整个Jupyter笔记本,由于某些原因,它起作用了。