I wrote this code to replace the nan values by the median of each column according to the value of the variable to explain:
data_target1 = data[data['target'] == 1]
data_target0 = data[data['target'] == 0]
for col_name in data.columns.values:
data_target1.loc[:,col_name] = data_target1[col_name].fillna(data_target1[col_name].median())
data_target0.loc[:,col_name] = data_target0[col_name].fillna(data_target0[col_name].median())
data = pd.concat([data_vae1,data_vae0], ignore_index=True)
data = data.sort_values(by='IDCLI_CALCULE')
data = data.reset_index(drop=True)
return data
I get during the execution :
SettingWithCopyWarning:A value is trying to be set on a copy of a slice from a DataFrame.
I understand the warning, I have read the documentation and yet I can't write a code without the warning appearing... Could someone explain my mistake and give me a solution to solve this problem?
I read these 2 pages and I understood that I had to use.loc to solve my problem but I remain blocked...
1- : Pandas: SettingWithCopyWarning
2- : Pandas SettingWithCopyWarning
It's probably a detail but I can't find the key to solve this little problem.
Thank you in advance
EDIT
After verification I was able to solve my problem using the "copy" method as suggested in the commentary.