我有一个包含一些分类变量的数据集,它们有一些缺失(NA / Null)。我想用该列的模式填充这些NA / Null。我累了以下事情,但这没用
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
仍然显示缺失值。
试试这个:
Data['Married'].fillna(Data['Married'].mode(), inplace=True)
或试试这个:
Data['Married'].fillna(Data['Married'].value_counts().index[0], inplace=True)
确保分类变量的dtype是对象或类别。