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
  • 52,436
  • 15
  • 51
  • 78
Hairy
  • 363
  • 1
  • 9
  • 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,365
  • 12
  • 24
0

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

Hairy
  • 363
  • 1
  • 9