RocketSocks22
RocketSocks22

Reputation: 411

Pandas dataframe .dropna() is not dropping NaN values

I have a set of features:

'SalePrice', 'OverallQual', 'GrLivArea', 'GarageCars', 'TotalBsmtSF', 'GarageArea', '1stFlrSF', 'FullBath', 'YearBuilt', 'TotRmsAbvGrd', 'YearRemodAdd', 'GarageYrBlt', 'Fireplaces', 'MasVnrArea', 'LotArea', 'LotFrontage', 'BsmtFinSF1', 'OpenPorchSF', 'WoodDeckSF', '2ndFlrSF', 'HalfBath', 'BsmtUnfSF', 'BsmtFullBath'

of which 3 features contain some NaN values. I would like to drop the rows with NaN values. To do so, I run

for item in features:
    train_data[item].dropna(inplace=True)
    print(train_data[item].isnull().values.any())

This gives the output:

False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False

This leads me to believe that all of the NaN values have been successfully dropped. However, when I then run

print(train_data[features].isnull().values.any())

Output:

True

My understanding was that by setting inplace=True that the dataframe would be manipulated in the current instance. However, when I inspect the train_data[features] after running the loop to drop null values, it is unchanged. Am I doing something wrong here?

Upvotes: 1

Views: 3919

Answers (1)

BENY
BENY

Reputation: 323226

IIUC

df=df.dropna(subset=features)

Upvotes: 5

Related Questions