pythonpython-3.xpandaschained-assignment

pandas set partial data and get SettingWithCopyWarning


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

Solution

  • 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.