sappy
sappy

Reputation: 840

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

Upvotes: 0

Views: 26

Answers (1)

jpp
jpp

Reputation: 164773

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.

Upvotes: 1

Related Questions