pandas set partial data and get SettingWithCopyWarning

29 Views Asked by At

I tried to set the partial data to -1, but I get a SettingWithCopyWarning.

I tried to find StackOverflow, but lots of answers use loc to solved.

The data comes from Kaggle Titanic.

import pandas as pd
train = pd.read_csv('data/train.csv')
y = train[["Survived"]]
y.loc[y["Survived"]  == 0,"Survived"] = -1
1

There are 1 best solutions below

3
jpp On BEST ANSWER

Your logic appears confused. Try this instead:

train.loc[train["Survived"]  == 0,"Survived"] = -1

There is no need to set y = train[['Survived']] and this is what is causing your warning.

You can read about how to use .loc accessor in the Pandas documentation.