0

I'm selecting a few rows from a dataframe as a new dataframe and trying to add a new column to that new dataframe with the selected rows. And I keep getting the warning, eventhough, I'm not trying set a value on a slice of that dataframe. The slice is already as a new dataframe. Why am I getting this message??

df=pd.DataFrame({'ID':[1,1,1,1,1,1,2,2,2,2,2,2],
             'V1':[1,5,8,20,40,77,10,15,6,4,80,30],
             'V2':[10,8,np.nan,16,np.nan,np.nan,80,10,22,np.nan,50,np.nan]})

col_impute = 'V2'
col_near='V1'
nearest_range = 0.2
values_to_replace = [np.nan,80]

rows_to_impute = df.loc[df[col_impute].isin(values_to_replace)]
rows_to_impute['LOW_RANGE'] = rows_to_impute[col_near]*(1-nearest_range)

C:\Users\User\AppData\Local\Temp\ipykernel_14932\783827865.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

To me it seems rows_to_impute should be an independent dataframe, not a view of df. What am I missing?

Ankhnesmerira
  • 1,386
  • 15
  • 29

1 Answers1

0

You can use

rows_to_impute = rows_to_impute.copy()

before adding a column to rows_to_impute

Raj Patel
  • 154
  • 6
  • OK, but I still don't understand why I would need to do that. to me it seems rows_to_impute should be an independent dataframe, not a view. What am I missing? – Ankhnesmerira May 06 '22 at 04:37
  • If we cont set a copy then the new data frame will be a slice of the original data frame and not a separate data frame these will not cause an issue unless and until you change the main data frame. for example, you create a new data frame based on the original data frame.. after creating a copy you change the values in the original data frame then the values of the new data frame will also change. [link](https://www.dataquest.io/blog/settingwithcopywarning/) – Raj Patel May 06 '22 at 04:38