Search code examples
pythondataframepandas-settingwithcopy-warning

Getting 'A value is trying to be set on a copy of a slice from a DataFrame.' while creating a new column


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?


Solution

  • You can use

    rows_to_impute = rows_to_impute.copy()
    

    before adding a column to rows_to_impute