"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"

Screen Link:

My Code:

# Define function for convert NaN to np.nan, '-' to False, other non zero string to True
def update_vals(value):
    if pd.isnull(value):
        return np.nan
    elif value == '-':
        return False
    else:
        return True

# Create new column and extract converted values from columns 'Contributing Factors. Dissatisfaction' 
# and 'Contributing Factors. Job Dissatisfaction' to 'dissatisfied'
intermediate = tafe_resignations.copy()

tafe_resignations['dissatisfied'] = intermediate.loc[:, ['Contributing Factors. Dissatisfaction', 'Contributing Factors. Job Dissatisfaction']].applymap(update_vals).any(axis=1, skipna=False)

tafe_resignations_up = tafe_resignations.copy()
tafe_resignations_up.info()

What I expected to happen:
I am trying to avoid the warning in the title of this post and have spent a decent amount of time trying to figure it out. What is the proper syntax for the code above to avoid “SettingWithCopyWarning:”

What are the principles behind this?

you simply need to add .copy() to the line where you apply your function.

tafe_resignations['dissatisfied'] = intermediate.loc[:, ['Contributing Factors. Dissatisfaction', 'Contributing Factors. Job Dissatisfaction']].applymap(update_vals).any(axis=1, skipna=False).copy()

what is happening is that in one step you have:

  1. selected a bunch of columns and rows from an existing dataframe
  2. altered the data by applying a function
  3. saved that altered data under a different name

Python cant tell if you want to:

  1. alter the selected data in the existing dataframe, and then also save that selected data as a new dataframe or series
    or
  2. use the selected data to create the new DF or series without altering the selected data itself.

when you use .copy() you are telling python to do option 2. So it selects the data, runs the function, creates a copy of those results, and then saves the copy as you choose without saving the changes to the original data.

1 Like

Thank you very much for taking the time to spell it out for me!

1 Like