Can anyone please help me to fix the warning whenever I type this code?
rh_can = df1.loc[(df1["hotel"] == "Resort Hotel") & (df1["is_canceled"] == 0)] ch_can = df1.loc[(df1["hotel"] == "City Hotel") & (df1["is_canceled"] == 0)]
rh_can['adr_pp'] = rh_can['adr'] / (rh_can['adults'] rh_can['children']) ch_can["adr_pp"] = ch_can["adr"] / (ch_can["adults"] ch_can["children"])
Warning Message: /usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:5: 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 """
CodePudding user response:
If you use df.loc function, it returns a view pointing to the data that corresponds to the condition. If the returned instance is updated, the source dataframe will be updated too.
This error occurs to prevent unintended modification of the source data. The error tells you to update the value using the index of the source instance, not the view instance.
You should use copy function to create a new dataframe if you don't want to update the source dataframe.
rh_can = df1.loc[(df1["hotel"] == "Resort Hotel") & (df1["is_canceled"] == 0)].copy()
ch_can = df1.loc[(df1["hotel"] == "City Hotel") & (df1["is_canceled"] == 0)].copy()
rh_can['adr_pp'] = rh_can['adr'] / (rh_can['adults'] rh_can['children'])
ch_can["adr_pp"] = ch_can["adr"] / (ch_can["adults"] ch_can["children"])