Solve – SettingWithCopyWarning even when using .loc[row_indexer.col_indexer] = value

SettingWithCopyWarning even when using .loc[row_indexer.col_indexer] = value is an error that occurs when you do not use a temp variable in order to hold the results and apply them back.

In this article I will try to explain the error and see how we can solve it, I will also some other solutions that worked for other developers who faced the same problems.

Describing SettingWithCopyWarning even when using .loc[row_indexer.col_indexer] = value

To replicate the error you can include the following lines to your code, or you can understand the code and create another version of it inside your script.

                                                                       #
row_index= value1['Total Population']=='*'
value1.loc[row_index,'Total Population'] = 4
                                                                       #

The error you will get is similar to the error bellow

                                                                       #
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 the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
                                                                       #

Bellow is the solution that worked for me, followed by other solutions that you can try if the first solution is not solving the error.

Solution 1 : use .copy() when creating the DataFrame

The error occurs when your data frame is created with a filter on top of a DataFrame.

The solution that I deemed best is using .copy() when creating the new DataFrame. Instead of using .loc directly.

                                                                       #
new_df_copy = df.loc[df.col1>2].copy()
new_df_copy.loc[2, 'new_column'] = 100
                                                                       #

Solution 2 : use a temp variable with DataFrame

Another simple trick is to use a temp variable in order to store and hold the variable temporarily, before applying it back to the column.

This simple manoeuvre will allow us to avoid updating the same set of values simultaneously in the DataFrame.

Thus at the end the resulting error will be gone forever. If this article has been of value to you, please consider donating to our Kofi account, by using the red button at the top of the page.

Solution 3 : use .replace() with the DataFrame

A solution that worked for a lot of developers including me is adding replace to the code that generates the error, make sure to apply this trick to your code as seen in the code bellow.

                                                                       #
value1[:, 'Total Population'] = value1[:, 'Total Population'].replace(to_replace='*', value=4)
                                                                       #

Summing-up

Guys, we are at the end of this article, I hope this guide helped you solve this error, I hope you do not have to waste a long portion of your time trying to solve this issue, I did my best with this one, please make sure to keep learning and keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/