Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How fill NA/Null for categorical Varibles in python using fillna() function

I Have one data set which contains some categorical variables and they have some missing(NA/Null). I Want to fill these NA/Nulls with Mode of that Column. I tired Following thing but This didn't work

MD=Data['Gender'].mode()
Data['Gender'].fillna(value=MD,inplace=True)


MD=Data['Married'].mode()
Data['Married'].fillna(value=MD,inplace=True)

MD=Data['Dependents'].mode()
Data['Dependents'].fillna(value=MD,inplace=True)

MD=Data['Self_Employed'].mode()
Data['Self_Employed'].fillna(value=MD,inplace=True)

MD=Data['Credit_History'].mode()
Data['Credit_History'].fillna(value=MD,inplace=True)

Gender                26
Married                6
Dependents            30
Education              0
Self_Employed         64
ApplicantIncome        0
CoapplicantIncome      0
LoanAmount             0
Loan_Amount_Term       0
Credit_History       100
Property_Area          0
Loan_Status            0

Still its Showing missing values.

like image 814
HardCoder Avatar asked Oct 22 '25 20:10

HardCoder


1 Answers

Try this:

Data['Married'].fillna(Data['Married'].mode(), inplace=True)

Or Try this:

Data['Married'].fillna(Data['Married'].value_counts().index[0], inplace=True)

Ensure that dtype of your categorical variables is object or category.

like image 112
YOLO Avatar answered Oct 25 '25 09:10

YOLO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!