user9549524
user9549524

Reputation: 57

copy of a slice from a DataFrame warning, loc method not working

There are many posts on this topic but unfortunately the solution provided (using loc) is not working for me. In my dataset, I have feature 'GarageYrBlt' which is the year in which the garage was built; and another feature 'YearBuilt' which is the year in which the house was built. The GarageYrBlt has some null values.

since GarageYrBlt and YearBuilt are strongly correlated, I am trying to replace the null values of GarageYrBlt by corresponding values of YearBuilt.

df.GarageYrBlt[df.GarageYrBlt.isnull()] = df.YearBuilt[df.GarageYrBlt.isnull()]

This gave the warning "A value is trying to be set on a copy of a slice from a DataFrame" So I tried the .loc method as shown below, but no change. I still get the same error. Any suggestions?

df.loc[:,"GarageYrBlt"][df.GarageYrBlt.isnull()] = df.loc[:,"YearBuilt"][df.GarageYrBlt.isnull()].copy()

Upvotes: 0

Views: 233

Answers (1)

John Zwinck
John Zwinck

Reputation: 249293

You need to do it in a single slice operation. So instead of this:

df.loc[:,"GarageYrBlt"][df.GarageYrBlt.isnull()] = df.loc[:,"YearBuilt"][df.GarageYrBlt.isnull()].copy()

Do this:

df.loc[df.GarageYrBlt.isnull(), "GarageYrBlt"] = df.loc[df.GarageYrBlt.isnull(), "YearBuilt"]

Or easier:

df.GarageYrBlt.fillna(df.YearBuilt, inplace=True)

Upvotes: 1

Related Questions