I have the below code in a function and I get SettingWithCopyWarning when rounding the numeric columns. I can't figure out what is wrong. Thanks
num_cols = df.select_dtypes(include=['number']).columns.tolist()
print("shape before elimination of 0 value rows", df.shape)
df = df.loc[(df[num_cols] != 0).any(axis=1)]
print("shape after elimination of 0 value rows", df.shape)
df.loc[:,num_cols] = df[num_cols].round(2)
Try with
num_cols = df.select_dtypes(include=['number']).columns.tolist()
df = df.loc[(df[num_cols] != 0).any(axis=1)].copy()
df.loc[:,num_cols] = df[num_cols].round(2)
Alternatively, if you're using Pandas 2, you can use
pd.options.mode.copy_on_write = True
and leave your code as is. (This setting will be the default in Pandas 3.)