How to fix the warnings SettingWithCopyWarning of pandas package

To Nha Notes | Sept. 25, 2023, 12:13 p.m.

The wanrning



/tmp/ipykernel_74793/168045246.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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df[df.Age >30]["City"] = "Tokyo"

 

How to fix

The SettingWithCopyWarning in pandas is a warning, not an error. It’s designed to alert you that you might be trying to perform an operation on a copy of a slice from a DataFrame, rather than the original DataFrame itself. This can lead to unexpected results because operations performed on a copy won’t affect the original DataFrame1.

Here are some ways to address this warning:

  1. Use .loc[row_indexer, col_indexer] to modify the DataFrame: The warning suggests this method. It ensures you’re modifying the original DataFrame and not a copy2.
df.loc[row_indexer, col_indexer] = value
  1. Create an explicit copy of the DataFrame: If you intend to work on a copy and don’t want to affect the original DataFrame, you can create an explicit copy using the .copy() method3.
df_copy = df.copy()
df_copy[some_condition] = value
  1. Suppress the warning: If you’re sure your operation is correct and want to suppress the warning, you can do so by adjusting pandas’ options45.
pd.options.mode.chained_assignment = None

Remember, it’s important to understand why you’re getting the warning before deciding how to handle it1. It’s generally not recommended to suppress the warning without understanding the underlying issue1.

 

Reference

python - How to deal with SettingWithCopyWarning in Pandas - Stack Overflow