pythondataframefillna

How can i fill the missing rows of dataframe column using average of another column?


I have a Dataframe with some missing values, I want to fill the missing values instead of dropping them.

But after writing the function, i am getting an error: ['MSSubCLass'] not in index.

LotFrontage is the column I want to fill. MSSubCLass is the column I want to fill with its averages.

My DataFrame table

My Fillna code

My fillna code

Error Message

def LotFrontage_fill(columns):
    LotFrontage = columns[0]
    MSSubCLass = columns[1]
    
    if pd.isnull(LotFrontage):
        if MSSubCLass == 20:
            return 75
        elif MSSubCLass == 30:
            return 60
        elif MSSubCLass == 40:
            return 50
        elif MSSubCLass == 45:
            return 55
        elif MSSubCLass == 50:
            return 65
        elif MSSubCLass == 60:
            return 75
        elif MSSubCLass == 70:
            return 65
        elif MSSubCLass == 75:
            return 68
        elif MSSubCLass == 80:
            return 80
        elif MSSubCLass == 85:
            return 70
        elif MSSubCLass == 90:
            return 70
        elif MSSubCLass == 120:
            return 40
        elif MSSubCLass == 160:
            return 20
        elif MSSubCLass == 180:
            return 20
        else:
            return 55
    else:
        return LotFrontage
    
    
train_df.LotFrontage = train_df[["LotFrontage","MSSubCLass"]].apply(LotFrontage_fill, axis = 1)

Solution

  • (Just so that the question can be marked as solved, I repeat my comment here as an answer)

    From the images you posted, it looks like you misspelled the column name in your code: It should be MSSubClass with a lowercase l and not MSSubCLass with an uppercase L.