I am working on the Kaggle Housing Prices project. I have tried for several hours to replace the NaN values in a string column 'BsmtQual'. For all houses with a SalePrice of less than 120000 that have a NaN value in the BsmtQual column, I want to replace it with 'Fa'.
df is my dataframe.
Fa_rng = df['SalePrice'] < 120000
I have attempted all the below and they did not change anything.
df.loc[Fa_rng,'BsmtQual'].fillna('Fa',inplace=True)
df.loc[Fa_rng,'BsmtQual'].replace('NaN','Fa',inplace=True)
df.loc[Fa_rng,'BsmtQual'].str.replace('NaN','Fa')
This one gets a warning saying "A value is trying to be set on a copy of a slice from a DataFrame" and does nothing.
df.loc[(df['BsmtQual'].isna()) & (df['SalePrice'] < 120000)].fillna('Fa',inplace=True)
How do I replace a NaN value in pandas for those houses to a 'Fa' string?
Don't use inplace
:
df.loc[Fa_rng & df['BsmtQual'].isna(),'BsmtQual'] = 'Fa'
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.