Created
March 24, 2023 23:39
-
-
Save john-adeojo/3dba38748d28144e22fad194d6854e63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define funtion te rplace missing vairbales | |
import pandas as pd | |
def impute_missing(df, test=False): | |
if test == False: | |
id_df = df['Id'] | |
y = df['SalePrice'] | |
df = df.drop(columns=['Id', 'SalePrice']) | |
else: | |
id_df = df['Id'] | |
df = df.drop(columns=['Id']) | |
df['LotFrontage'] = df['LotFrontage'].fillna(0) | |
df['Alley'] = df['Alley'].fillna('No Alley') | |
df['MasVnrType'] = df['MasVnrType'].fillna('None') | |
df['MasVnrArea'] = df['MasVnrArea'].fillna(0) | |
df['BsmtQual'] = df['BsmtQual'].fillna('None') | |
df['BsmtCond'] = df['BsmtCond'].fillna('None') | |
df['BsmtExposure'] = df['BsmtExposure'].fillna('None') | |
df['BsmtFinType1'] = df['BsmtFinType1'].fillna('None') | |
df['BsmtFinType2'] = df['BsmtFinType2'].fillna('None') | |
df['Electrical'] = df['Electrical'].fillna('SBrkr') | |
df['FireplaceQu'] = df['FireplaceQu'].fillna('None') | |
df['GarageType'] = df['GarageType'].fillna('None') | |
df['GarageYrBlt'] = df['GarageYrBlt'].fillna(9999) | |
df['GarageFinish'] = df['GarageFinish'].fillna('None') | |
df['GarageQual'] = df['GarageQual'].fillna('None') | |
df['GarageCond'] = df['GarageCond'].fillna('None') | |
df['PoolQC'] = df['PoolQC'].fillna('None') | |
df['Fence'] = df['Fence'].fillna('None') | |
df['MiscFeature'] = df['MiscFeature'].fillna('None') | |
df['MSZoning'] = df['MSZoning'].fillna('RL') | |
df['Utilities'] = df['Utilities'].fillna('AllPub') | |
df['Exterior1st'] = df['Exterior1st'].fillna('VinylSd') | |
df['Exterior2nd'] = df['Exterior2nd'].fillna('VinylSd') | |
df['BsmtFinSF1'] = df['BsmtFinSF1'].fillna(0) | |
df['BsmtFinSF2'] = df['BsmtFinSF2'].fillna(0) | |
df['BsmtUnfSF'] = df['BsmtUnfSF'].fillna(0) | |
df['BsmtUnfSF'] = df['BsmtUnfSF'].fillna(0) | |
df['TotalBsmtSF'] = df['TotalBsmtSF'].fillna(0) | |
df['BsmtFullBath'] = df['BsmtFullBath'].fillna(0) | |
df['BsmtHalfBath'] = df['BsmtHalfBath'].fillna(0) | |
df['KitchenQual'] = df['KitchenQual'].fillna('TA') | |
df['Functional'] = df['Functional'].fillna('Typ') | |
df['Functional'] = df['Functional'].fillna('Typ') | |
df['GarageCars'] = df['GarageCars'].fillna(0) | |
df['GarageArea'] = df['GarageArea'].fillna(0) | |
df['SaleType'] = df['SaleType'].fillna('WD') | |
# convert int to object | |
df['YearBuilt'] = df['YearBuilt'].astype('object') | |
df['YearRemodAdd'] = df['YearRemodAdd'].astype('object') | |
df['GarageYrBlt'] = df['GarageYrBlt'].astype('object') | |
df['YrSold'] = df['YrSold'].astype('object') | |
if test == False: | |
return df, y, id_df | |
else: | |
return df, id_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment