Replace nan-values with the mean of their column/attribute

Viewed 40

I have tried with everything I can come up with and would appreciate some help! :) This is a method that's gonna return an imputed part of a data frame

from statistics import mean
from unicodedata import numeric


def imputation(df, columns_to_imputed):
    
    
    # Step 1: Get a part of dataframe using columns received as a parameter.
    import pandas as pd
    import numpy as np

    df.set_axis(['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome'], axis=1, inplace=True)#Sätter rubrikerna

    part_of_df = pd.DataFrame(df.filter(columns_to_imputed, axis=1))
    part_of_df = part_of_df.drop([0], axis=0)
   

    #Step 2: Change the zero values in the columns to np.nan
    part_of_df = part_of_df.replace('0', np.nan)

    # Step 3: Change the nan values to the mean of each attribute (column). 
                  #You can use the apply(), fillna() functions.

    part_of_df = part_of_df.fillna(part_of_df.mean(axis=0)) #####Ive tried everything on this row, can't get it to work. I want to fill each nan-value with the mean of the column its in..

    return part_of_df  ####Im returning this part to see if the nans are replaced but nothings happened...

enter image description here

1 Answers

You were on the right track, you just need to make a small change. Here I created a sample Df and introduced some NaNs:

dummy_df = pd.DataFrame({"col1":range(5), "col2":range(5)})
dummy_df['col1'][1] = None
dummy_df['col1'][3] = None
dummy_df['col2'][4] = None

and got this:

Initial DataFrame

Disclaimer: Don't use my method of value assignment. Use proper indexing through loc.

Now, I use apply() and lambda to iterate over each column and fill NaNs with the mean value:

dummy_df = dummy_df.apply(lambda x: x.fillna(x.mean()), axis=0)

This gives me:

enter image description here

Hope this helps!

Related