Why the missing values aren't filled?

Viewed 51

While using the pandas filling method on a data frame I am using median values of that specific column to fill the values, instead of filling it is being doubled. Here's the missing values before :

id                                     0
perc_premium_paid_by_cash_credit       0
age_in_days                            0
Income                                 0
Count_3-6_months_late                  0
Count_6-12_months_late                 0
Count_more_than_12_months_late         0
application_underwriting_score      2974
no_of_premiums_paid                    0
sourcing_channel                       0
residence_area_type                    0
target                                 0
dtype: int64

Hence I used the following to fill the Na values:

train['application_underwriting_score'] = train['application_underwriting_score'].fillna(train['application_underwriting_score'].median(),inplace=True)

But instead of getting filled I am getting more Na values:

id                                      0
perc_premium_paid_by_cash_credit        0
age_in_days                             0
Income                                  0
Count_3-6_months_late                   0
Count_6-12_months_late                  0
Count_more_than_12_months_late          0
application_underwriting_score      79853
no_of_premiums_paid                     0
sourcing_channel                        0
residence_area_type                     0
target                                  0
dtype: int64

I already checked the median, which is coming out to be 92.1. What could be the possible fault in my code?

1 Answers

You need remove inplace=True:

train['application_underwriting_score'] = train['application_underwriting_score'].fillna(train['application_underwriting_score'].median())

Or:

train['application_underwriting_score'].fillna(train['application_underwriting_score'].median(), inplace=True)
Related