To Nha Notes | Sept. 25, 2023, 12:13 p.m.
/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"
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:
df.loc[row_indexer, col_indexer] = value
df_copy = df.copy() df_copy[some_condition] = value
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