0

I need to use the following code:

raw_data.loc[(raw_data['PERMNO']==10006)&(raw_data['month']>=50)&(raw_data['month']<=100)]['resi']=raw_data['RET']-raw_data['ewretd']

that is based on the conditions to calculate column 'resi'.

But I keep getting warnings like

D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: 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 """Entry point for launching an IPython kernel.

How to correct this?

Canvas
  • 41
  • 5

1 Answers1

0

Try adding df.copy:

raw_data = raw_data.copy()
raw_data.loc[(raw_data['PERMNO']==10006)&(raw_data['month']>=50)&(raw_data['month']<=100), 'resi'] = raw_data['RET'] - raw_data['ewretd']
U13-Forward
  • 69,221
  • 14
  • 89
  • 114