By Jhon Snow


2019-03-22 15:15:40 8 Comments

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.

0 comments

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] How to select rows from a DataFrame based on column values?

23 Answered Questions

[SOLVED] Renaming columns in pandas

18 Answered Questions

[SOLVED] How to iterate over rows in a DataFrame in Pandas?

14 Answered Questions

[SOLVED] "Large data" work flows using pandas

13 Answered Questions

[SOLVED] How to deal with SettingWithCopyWarning in Pandas?

3 Answered Questions

[SOLVED] Dataframe Warning : SettingWithCopyWarning in python

2 Answered Questions

[SOLVED] Pandas: SettingWithCopyWarning:

  • 2017-07-18 14:55:37
  • salhin
  • 1382 View
  • 4 Score
  • 2 Answer
  • Tags:   python pandas

1 Answered Questions

[SOLVED] Pandas map to a new column, SettingWithCopyWarning

  • 2017-02-08 06:07:18
  • Ik-Hwan Kim
  • 6902 View
  • 17 Score
  • 1 Answer
  • Tags:   python pandas

1 Answered Questions

[SOLVED] How to avoid SettingWithCopyWarning in pandas?

  • 2016-12-10 13:55:44
  • Dimitris Poulopoulos
  • 872 View
  • 1 Score
  • 1 Answer
  • Tags:   pandas python-3.5

2 Answered Questions

[SOLVED] python pandas 0.16: SettingWithCopyWarning incorrectly reported

  • 2015-06-25 16:55:36
  • Pythonista anonymous
  • 162 View
  • 0 Score
  • 2 Answer
  • Tags:   python pandas

Sponsored Content