简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM