0

I want to copy one column into a new one. I use this code:

df['income10']=df['income'].copy(deep=False)

I get this error:

/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

By adding shallow copy I thought I would've prevented the error.

rafaelc
  • 57,686
  • 15
  • 58
  • 82
Hairy
  • 393
  • 1
  • 10
  • Your df is a subset from another dataframe right ? – BENY Jan 31 '19 at 19:18
  • Use `df = df.assign(income10 = df.income)`. It will create a new object which wont be a reference. Then again, might be best to fix the problem further up in your code. – ALollz Jan 31 '19 at 19:39

2 Answers2

1

Try doing this way:

df['income10']=df.loc[:, ['income']]
taurus05
  • 2,491
  • 15
  • 28
0

I reran the whole Jupyter notebook and it worked for some reason.

Hairy
  • 393
  • 1
  • 10