Error in imputing missing values using fillna

pandas
python

#1

#4, pivot table
Sir, How will i impute the variable(impute_grps) into the dataframe to replace the Nan values to respective LoanAmounts.
i tried the following code:

df["LoanAmount"].fillna(value=impute_grps,inplace= True)

#2

hI @devesh35,

You can use this command

train['Loan_Amount_Term'].fillna(train['Loan_Amount_Term'].mode()[0], inplace=True)

You can also refer this guide which provides a benchmark solution for the loan prediction problem.


#3

I want to impute based on the calculation of their mean of gender,married,self_ employed which is calculated in the variable impute_grps.


#4

Hi @devesh35,

You can use the below line of code

impute_grps = df.pivot_table(values=["LoanAmount"], index=["Gender","Married","Self_Employed"], aggfunc=np.mean)

temp=df.set_index(['Gender', 'Married', 'Self_Employed']).LoanAmount.fillna(impute_grps.LoanAmount).reset_index()

This will give you a new dataframe temp, which has the desired loan amount values. You can drop the loan amount column from df and concat the loan amount column from temp.

On a side note, preferably post the code and error in text format (instead of image) so that we can use the same code to generate the error and provide a solution.